Lisp illegal function call,

2019-07-30 17:09发布

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

2条回答
祖国的老花朵
2楼-- · 2019-07-30 17:45

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))
查看更多
Juvenile、少年°
3楼-- · 2019-07-30 17:50

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

查看更多
登录 后发表回答