2012-10-18 23 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)))