Lisp, instructions not working in defun [duplicate

2019-01-07 01:26发布

问题:

This question already has an answer here:

  • Common lisp error: “should be lambda expression” 4 answers

I'm trying to make a function that changes infix input to prefix eg : (x + 1) as input outputted as (+ x 1).

So here is my code for the moment :

(setq x '(Y + 1))
(if (listp x ) (list (second x) (first x) (first (last x))) x)

so it returns (+ Y 1) if I input a list and the user input if it's not a list.

However, the problem is that I can't get this code working in a function :

(defun prefixToInfix (x)(
   (if (listp x ) (list (second x) (first x) (first (last x))) x)
   )
 )

the function is indeed created but when I call it

(prefixtoinfix '(Y + 1))

I get an error

Error: Illegal function object: (IF (LISTP X) (LIST # # #) X).
[condition type: TYPE-ERROR]

I don't know why my if statement works in the main program but doesn't when I run it from my function.

回答1:

What you are missing is that in Lisp parentheses are meaningful.

In C/Java/Python &c, the following expressions are the same:

a+b
(a+b)
(a)+(b)
(((a)+(b)))
(((((a)+(b)))))

In Lisp, the following expressions are very different:

  • a --- a symbol
  • (a) --- a list with a single element, which is the symbol a
  • (1 (2)) --- a list of two elements:
    1. number 1
    2. list of of length 1, containing number 2

In your case, function (note indentation and paren placement!)

(defun prefixToInfix (x)
  ((if (listp x) (list (second x) (first x) (first (last x))) x)))

has extra parens around if (and this causes the whole if form to be interpreted as a function, with disastrous results), and should be (note line breaks and indentation - lispers do not count parens, they look at indentation to understand the code, see http://lisp-lang.org/style-guide/)

(defun prefix-to-infix (x)
  (if (listp x)
      (list (second x)
            (first x)
            (first (last x)))
      x))

PS. See also recommendations in want to learn common lisp.