Lisp illegal function call,

你说的曾经没有我的故事 提交于 2019-12-12 22:38:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!