2012-10-09 24 views
-3

下列檢查該功能用於在列表中的號碼。例如,在這裏它正在尋找12.如果12是存在的,它返回T(真),如果它不存在,則返回NIL。我試圖理解語法,但它是一種困惑我。有沒有人可以幫助和描述這段代碼用簡單的英語來做什麼?如何做到這一點Lisp代碼工作?

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工作或者它有什麼作用?

回答

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)))) 

也許,你會如果你解釋什麼是你想幹什麼獲得更好的運氣,這個功能是錯的,它是乞討這個問題。

+0

由於該函數應該找到的元素或原子,其是12在這種情況下該列表(1 2 3 4 5 6 12)如果它的存在的函數應該返回真,如果不是,它應該返回零內 –