Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

syntax error - Lisp, instructions not working in defun


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...