问题
The code below keeps throwing the following error:
caught ERROR:
illegal function call
(LET ((SOLUTION 'NIL) (FIRST 0) (SECOND 0))
(DOLIST (EL LST)
(IF (NUMBERP EL)
(PUSH EL SOLUTION)
((SETF #) (SETF #) (PUSH # SOLUTION))))
(CAR SOLUTION))
Can anyone see why? Syntactically I can't see anything wrong with it. Note: I'm using sbcl.
My code:
(defun evalpostfix (lst)
(let ((solution '())
(first 0)
(second 0))
(dolist (el lst)
(if (numberp el) ;if
(push el solution) ;then
((setf second (pop solution)) ;else
(setf first (pop solution))
(push (funcall el first second) solution))))
(car solution)))
回答1:
((setf second (pop solution))
- two opening parentheses? Why? The syntax for IF
with an else form is:
(if test-form then-form else-form)
A form cannot begin with two parentheses - there is only one exception: ((lambda (x) (+ x 1)) 2)
.
If you want to group more than one expression use something like (progn a b c ... z)
.
回答2:
Indeed, in order to group several forms together, use the special operator progn.
You can make a step futher and group consequtive setf calls together:
(setf second (pop solution)
first (pop solution))
来源:https://stackoverflow.com/questions/16002497/lisp-illegal-function-call