Lisp,虽然函数未定义错误与CLISP?

Dan*_*SFT 2 lisp loops clisp common-lisp while-loop

我正在研究LISP中的一个程序,使用CLISP来运行程序.

我的函数中有一个while语句,但CLISP正在返回

*** - EVAL: undefined function WHILE
Run Code Online (Sandbox Code Playgroud)

功能没什么特别的,

(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)))
Run Code Online (Sandbox Code Playgroud)

sep*_*p2k 5

while在Common Lisp中没有带有名称的函数或宏(或"语句"),因此CLISP向您提供该错误消息是正确的.

也许你打算使用loop宏,它接受while作为其语法的一部分.


Ani*_*nge 5

你错过了loop你的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)))
    (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)))
Run Code Online (Sandbox Code Playgroud)


Vat*_*ine 5

whileCommon Lisp中没有标准的循环构造,Emacs Lisp中有一种。但是,如果您想要一个,则做到这一点相对简单。

(defmacro while (condition &body body)
  `(loop while ,condition
      do (progn ,@body)))
Run Code Online (Sandbox Code Playgroud)