2012-10-18 21 views
1

我正在LISP中使用CLISP運行該程序的程序。Lisp,雖然CLISP函數未定義錯誤?

我的功能中有一個while語句,但CLISP正在恢復

*** - EVAL: undefined function WHILE 

功能不花哨,

(defun heap-insert (heap item key) 
    "Put an item into a heap. [Page 150 CL&R]." 
    ;; Note that ITEM is the value to be inserted, and KEY is a function 
    ;; that extracts the numeric value from the item. 
    (vector-push-extend nil heap) 
    (let ((i (- (length heap) 1)) 
    (val (funcall key item))) 
    (while (and (> i 0) (>= (heap-val heap (heap-parent i) key) val)) 
     do (setf (aref heap i) (aref heap (heap-parent i)) 
       i (heap-parent i))) 
     (setf (aref heap i) item))) 

回答

4

你之前,你的while

試錯過loop

(defun heap-insert (heap item key) 
    "Put an item into a heap. [Page 150 CL&R]." 
    ;; Note that ITEM is the value to be inserted, and KEY is a function 
    ;; that extracts the numeric value from the item. 
    (vector-push-extend nil heap) 
    (let ((i (- (length heap) 1)) 
    (val (funcall key item))) 
    (loop while (and (> i 0) (>= (heap-val heap (heap-parent i) key) val)) 
     do (setf (aref heap i) (aref heap (heap-parent i)) 
       i (heap-parent i))) 
     (setf (aref heap i) item))) 
5

有沒有功能或宏(或「聲明」)的名稱Common Lisp中的while,所以CLISP是正確的給你那個錯誤信息。

也許你打算使用loop宏,它接受while作爲其語法的一部分。

4

沒有標準while循環結構Common Lisp中,有一個在的Emacs Lisp。但是,如果你想要一個,那麼做到這一點相對簡單。

(defmacro while (condition &body body) 
    `(loop while ,condition 
     do (progn ,@body)))