-3

下面的这个函数检查列表中的数字。例如,这里它正在寻找 12。如果 12 存在,则返回T(true),如果不存在,则返回 NIL。我试图理解语法,但这让我感到困惑。有没有人可以帮助并用简单的英语描述这段代码的作用?

1> (defun an (&rest n)
    (block nil
      (setq x (car n))
      (setq n (cdr n))
      (loop (< x 100)
      (setq n (cdr n))
      (if (eq x 2) (return (eq (car n) 12))) (setq x (1- x)))))
AN
2> (an 2 3 4 5 66 7)
NIL
3> (an 2 3 12 3 4 5)
T

附加问题:它是如何&rest工作或做什么的?

4

1 回答 1

1

如果您使用的是 SLIME,您可以M-xslime-macroexpand-all在该点位于块形式的最后一个括号上时执行此操作。你会得到这样的东西:

(BLOCK NIL
  (SETQ X (CAR N))                      ; save the first element in X, while declaring
                                        ; a special variable by that name
  (SETQ N (CDR N))                      ; set N to the second cons of the list
  (BLOCK NIL
    (TAGBODY
     #:G892
      (PROGN
       (< X 100)                        ; Useless, has no impact on your code
       (SETQ N (CDR N))                 ; set N to the third cons of the list
       (IF (EQ X 2)
           (RETURN-FROM NIL (EQ (CAR N) 12))) ; return from the innermost block NIL
                                        ; however, since there's no more code in the
                                        ; outermost block NIL, this will return from
                                        ; it too.
       (SETQ X (1- X)))                 ; decrement value in X. It may happen so by
                                        ; chance that, if initially X was larger than 2
                                        ; the above condition will trigger
      (GO #:G892))))

也许,如果你解释了你想要做什么,你会得到更好的运气,这个功能非常错误,以至于它在乞求这个问题。

于 2012-10-09T15:51:35.947 回答