Re: Re: case æ§æã®ãã¼ãæ¬å¼§ã§ãããã¨ä½ãå¤ããã®ã
åè:
otherwise-clause ãªãã® case ããã£ããå®è£ ãã¦ã¿ãã¨ããããªæãã
(defmacro %with-ca/dr (obj &body body) `(let* ((#1=#:obj ,obj) (car (car #1#)) (cdr (cdr #1#))) ,@body)) => %with-ca/dr (defmacro case%0 (key-form &rest clauses) `(let ((#1=#:key ,key-form)) ,(labels ((expand-clauses (clauses) (if (null clauses) nil (%with-ca/dr (car clauses) `(if (,(if (consp car) 'member 'eql) #1# ',car) (progn ,@cdr) ,(expand-clauses (cdr clauses))))))) (expand-clauses clauses)))) => case%0 (setq letter 'u) => u (macroexpand-1 `(case%0 letter (s 's) (t 't) (u 'u) (otherwise 'nothing))) => (let ((#1=#:key letter)) (if (eql #1# 's) (progn 's) (if (eql #1# 't) (progn 't) (if (eql #1# 'u) (progn 'u) (if (eql #1# 'otherwise) (progn 'nothing) nil))))) => t
ããã« otherwise-clause ã使ããããã«ãããã ãã©ãotherwise-clause ã£ã¦ãªã«ãï¼ã¨ããã®ã確èªããã
Macro CASE, CCASE, ECASE
Syntax:
case keyform {normal-clause}* [otherwise-clause] => result*
ccase keyplace {normal-clause}* => result*
ecase keyform {normal-clause}* => result*
normal-clause::= (keys form*)
CLHS: Macro CASE, CCASE, ECASE
otherwise-clause::= ({otherwise | t} form*)
clause::= normal-clause | otherwise-clause
- clause ã®å é ã {otherwise | t}
- case å¼ã®æå¾ã® clause 㯠otherwise-clause ãããããªã
ã¨ãããã¨ã«ãªã£ã¦ãã
ãã¤ã³ãã¯ãclauses ã®æå¾ä»¥å¤ã§ãå é ã {otherwise|t} 㪠clauseããç¾ããå ´åã«ã©ãããããå¥ã®è¨ãæ¹ããã㨠otherwise-clause 㯠clauses ã®æå¾ã§ãªããã°ãªããªãã®ãã©ããã
clauses ã®æå¾ãã©ããã *æ°ã«ããªã* ã§ãå é ã {otherwise|t} ãªã otherwise-clauseãã«ããã¨ãããªãããã¶ã PAIP ã®èª¬æã¨ããã®ã¯ããããå®è£ ã®ãã¨ãè¨ã£ã¦ããã ã¨æãã
(defmacro case%1 (key-form &rest clauses) `(let ((#1=#:key ,key-form)) ,(labels ((expand-clauses (clauses) (if (null clauses) nil (%with-ca/dr (car clauses) (cond ((member car '(otherwise t) :test #'eq) `(progn ,@cdr)) (t `(if ,(if (consp car) `(member #1# ',car) `(eql #1# ',car)) (progn ,@cdr) ,(expand-clauses (cdr clauses))))))))) (expand-clauses clauses)))) => case%1 (macroexpand-1 '(case%1 letter (s 's) (t 't) (u 'u) (otherwise 'nothing))) => (let ((#1=#:key letter)) (if (eql #1# 's) (progn 's) (progn 't))) => t
次㫠clauses ã®æå¾ã ã otherwise-clause ãããããªããã¨ããå®è£ ããã¶ã SBCL ããããéä¸ã«åºã¦ãã (t ...) ã (otherwise ...) ãä½ãè¨ããã« (if (eql #:key 't) (progn ...) ..) ã«å±éãã¦ããããæ°ãå©ããªããã¨ããã¨ãããã
(defmacro case%2 (key-form &rest clauses) `(let ((#1=#:key ,key-form)) ,(labels ((expand-clauses (clauses) (%with-ca/dr (car clauses) (if (= (length clauses) 1) ;; æå¾ã® clause (if (member car '(otherwise t) :test #'eq) `(progn ,@cdr) `(if (,(if (consp car) 'member 'eql) #1# ',car) (progn ,@cdr))) ;; æå¾ä»¥å¤ã® clause `(if (,(if (consp car) 'member 'eql) #1# ',car) (progn ,@cdr) ,(expand-clauses (cdr clauses))))))) (expand-clauses clauses)))) => case%2 (macroexpand-1 '(case%2 letter (s 's) (t 't) (u 'u) (otherwise 'nothing))) => (let ((#1=#:key letter)) (if (eql #1# 's) (progn 's) (if (eql #1# 't) (progn 't) (if (eql #1# 'u) (progn 'u) (progn 'nothing))))) => t
`(if (,(if (consp car) 'member 'eql) #1# ',car) (progn ,@(cdar clauses)) ..) ã2ååºã¦ããããããããããã£ã¦ãªããã©ã説æç¨ãªã®ã§è¨±ãã¦ãã ããã
ä½è«: clauses ãå±éããã®ã« mapcar ã¨ã使ã£ã¦ãã¨ãæå¾ã® clause ã ãå¥ã®å¦çã¨ããã®ãããã«ããã£ãã ãããªãã¨æã£ãããã¾ããã
ããã§ãéä¸ã«åºã¦ãã (t ...) ã (otherwise ...) ã¯ã©ããããï¼ã¨ãããã¨ã«ãªãã®ã ãã©
keys---a designator for a list of objects. In the case of case, the symbols t and otherwise may not be used as the keys designator. To refer to these symbols by themselves as keys, the designators (t) and (otherwise), respectively, must be used instead.
CLHS: Macro CASE, CCASE, ECASE
ãå³å¯ã«é©ç¨ãããªãã¨ã©ã¼ã§ããããããªæ°ããããã©ããèéãå©ããªããã¨ãã空æ°èªããã£ã¦è¨ãããããã¨ãããä»æ§ã§å®ãããã¦ãªãã¨ã©ã¼æããã£ã¦ã©ããªã®ãã
ã¨ãã訳ã§è¦åãåºããã¨ã«ãããCCL ã¯ã§ããåã
(defmacro case%3 (key-form &rest clauses) `(let ((#1=#:key ,key-form)) ,(labels ((expand-clauses (clauses) (%with-ca/dr (car clauses) (if (= (length clauses) 1) ;; æå¾ã® clause (if (member car '(otherwise t) :test #'eq) `(progn ,@cdr) `(if (,(if (consp car) 'member 'eql) #1# ',car) (progn ,@cdr))) ;; æå¾ä»¥å¤ã® clause (progn (if (member car '(otherwise t) :test #'eq) (warn "コï¾ï¾ï½§")) `(if (,(if (consp car) 'member 'eql) #1# ',car) (progn ,@cdr) ,(expand-clauses (cdr clauses)))))))) (expand-clauses clauses)))) => case%3 (macroexpand-1 '(case%3 letter (s 's) (t 't) (u 'u) (otherwise 'nothing))) ;; simple-warning: コï¾ï¾ï½§ => (let ((#1=#:key letter)) (if (eql #1# 's) (progn 's) (if (eql #1# 't) (progn 't) (if (eql #1# 'u) (progn 'u) (progn 'nothing))))) => t
ãããCCL ã¨ã SBCL ã¨ãéãæåã«ãªã£ã¡ãã£ãã
ã¡ãªã¿ã«: xyzzy ã®å ´å
(macroexpand-1 '(case letter (s 's) (t 't) (u 'u) (otherwise 'nothing))) => (let ((#1=#:key letter)) (if (eql #1# 's) (progn 's) (progn 't))) => t
ï¼ãã®ã¨ã³ããªã®ï¼ChangeLog
- 2010-11-04
- ãã¼ãæ¬å¼§ã§ããã£ãå ´åã®ãã¨ãå¿ãã¦ãã®ã§ä¿®æ£
- å¾®å¦ã«ãªãã¡ã¯ã¿ãªã³ã°ãã
- ã³ã¼ã㯠gist ã«ãã¨ãã°è¯ãã£ãã¨æã£ã