2

CLISP を使用してプログラムを実行し、LISP でプログラムを作成しています。

私の関数には 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

3 に答える 3

5

Common Lisp にはその名前の関数やマクロ (または「ステートメント」) がwhileないため、CLISP がそのエラー メッセージを表示するのは正しいことです。

構文の一部としてloop受け入れるマクロを使用するつもりだったのかもしれません。while

于 2012-10-18T19:34:41.497 に答える
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)))
于 2012-10-18T19:34:56.737 に答える
5

Common Lisp には標準のwhileループ構造はありませんが、Emacs Lisp には標準のループ構造があります。ただし、必要な場合は、それを実現するのは比較的簡単です。

(defmacro while (condition &body body)
  `(loop while ,condition
      do (progn ,@body)))
于 2012-10-19T10:17:25.320 に答える