2012-10-18 1 views
1

Je travaille sur un programme dans LISP, en utilisant CLISP pour exécuter le programme.Lisp, Alors que la fonction d'erreur non définie avec CLISP?

Ma fonction a une while en elle, mais CLISP revient

*** - EVAL: undefined function WHILE 

Fonction n'a rien d'extraordinaire,

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

Répondre

4

Vous avez manqué loop avant while

essayer:

(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

Il n'y a pas la fonction ou macro (ou "déclaration") avec le nom while dans Common Lisp, donc CLISP a raison de vous donner ce message d'erreur. Peut-être que vous vouliez utiliser la macro loop, qui accepte while dans le cadre de sa syntaxe.

4

Il n'y a pas while norme en boucle construction en Common Lisp, il y a un Emacs Lisp. Cependant, si vous en voulez un, il est relativement simple d'y arriver.

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