1

I am just starting to learn lisp and am a bit puzzled as to why the compiler does not evaluate a simple integer enclosed in parentheses to the value of that integer.

In the REPL, when I do (+ 3 2) I get 5, but when I do (+ 3 (2)) I get an error, whereas I think the value of the expression (2) should also be 2. Clearly there is something important here that I am unable to lay my finger on - what's the difference between 2 and (2)? Any help would be most appreciated since this seems fundamental to the way lisp works.

ahron
  • 803
  • 6
  • 29
  • This unfortunately makes all no sense, as Lisp does not and should not behave as you describe. – Rainer Joswig Jan 29 '17 at 16:14
  • Yes, you're right. I made a mistake in a part of the question: the first part [where I wrote about (2) returning 2] was in a different lisp. I mistakenly presumed since the behavior of the second part [(+ 3 (2)) being different from (+ 3 2)] was consistent across both lisps, the behavior of the first part must also be the same. Apologies for the confusion. I have now removed the first part. Nevertheless, the core of the question still remains (the same). – ahron Jan 29 '17 at 16:35
  • 1
    `I think the value of the expression (2) should also be 2` - why would you 'think' that? Why not read a Lisp introduction to get the basics? This is a good beginner introduction I would recommend: https://www.cs.cmu.edu/~dst/LispBook/ A free download of the book is available on that page. – Rainer Joswig Jan 29 '17 at 16:38
  • I actually am reading from a book (the one by Paul Graham). In the second chapter, where he actually starts with the language, I see statements of the form I wrote, and so tried a few of my own - including the one that didn't work. Nowhere in the next few sections of that chapter was it discussed why `(2)` would be different from `2`, hence my question on SO. – ahron Jan 29 '17 at 16:44
  • Why not read page 8 and 9 of that book (the one by Paul Graham)? – Rainer Joswig Jan 29 '17 at 16:50
  • I have. "All lisp expressions are either atoms, like 1, or lists, which consist of zero or more expressions enclosed in parentheses". So why is (2) not a valid list? I understand that if it tried evaluating the contents enclosed witin the parentheses as a function, it would find 2 is not a function. But the value of the argument `2` is 2, so ought that not be the value of the expression, if `2` is the sole content of that expression? – ahron Jan 29 '17 at 16:57
  • page 8, top left: `in the expression (+ 2 3), the + is called the operator and the numbers 2 and 3 are the arguments... this is called prefix notation' ---- now look at (2) --- what would that be? – Rainer Joswig Jan 29 '17 at 17:01
  • In that expression, yes, I fully agree. I am not that stupid. What I have been looking for is the rule that says the first expression of every list must always be an operator. Since that seems to be the rule that would explain my conundrum. I am now on Page 31 of the online pdf of the book you kindly linked to, where at the bottom is an example of a valid list, (AADVARK), but entering that in the REPL gives an error. Why? – ahron Jan 29 '17 at 17:07
  • Because of page 8 (the book by Paul Graham): `in the expression (+ 2 3), the + is called the operator and the numbers 2 and 3 are the arguments... this is called prefix notation`. Is AADVARK an operator? – Rainer Joswig Jan 29 '17 at 17:10
  • What I am trying to ask, is if there is a rule that states the first element of a valid list must be an operator? Because I have now gone through many more pages of the book you linked and it isn't there. – ahron Jan 29 '17 at 17:16
  • lists can have any contents. Valid Lisp expressions are described on page 8 of Graham's book: `in the expression (+ 2 3), the + is called the operator and the numbers 2 and 3 are the arguments... this is called prefix notation`. I still don't understand why you 'think' that (2) and 2 should evaluate the same. There is no logic behind it so far. – Rainer Joswig Jan 29 '17 at 17:18
  • So if a list can have any contents, how is it evaluated, if the first expression within the list is not an operator? – ahron Jan 29 '17 at 17:23
  • It does not get evaluated. It's an error. – Rainer Joswig Jan 29 '17 at 17:25
  • Are the examples of lists on pages 31 and 32 of the book you linked are basically of invalid lists that would not actually evaluate? – ahron Jan 29 '17 at 17:29
  • They are valid lists. Not valid Lisp programs. Not every list is a valid Lisp program. `(2 + 2)` is a valid list, but not a valid Lisp program, since Lisp expects an operator as the first item of that list. – Rainer Joswig Jan 29 '17 at 17:29
  • How would those lists get processed, if the first element isn't an operator? – ahron Jan 29 '17 at 17:32
  • It does not get processed. It's an error. – Rainer Joswig Jan 29 '17 at 17:32
  • So a list of data only cannot be entered in lisp? – ahron Jan 29 '17 at 17:33
  • If the list you want to evaluate is not a valid Lisp program, Lisp will give you an error. If you give a Java compiler a program with syntax errors, it will complain, too. What's surprising? – Rainer Joswig Jan 29 '17 at 17:37
  • What's surprising is the fact that the book mentions so many examples of lists which cannot actually get processed. – ahron Jan 29 '17 at 17:40
  • But I think I get the idea now. And I thank you tremendously for your enormous patience!!! – ahron Jan 29 '17 at 17:40
  • `What's surprising is the fact that the book mentions so many examples of lists which cannot actually get processed.` That's because these lists are valid data, very useful, but they are not valid Lisp programs. Lisp can compute with these lists just fine, but as data, not as programs. As you found out now: a) lists can have any contents b) valid Lisp programs use prefix notation. You tell the Lisp evaluator that a list is data by quoting it. Page 10ff in Graham's book. – Rainer Joswig Jan 29 '17 at 17:42
  • Yes. And c) everything entered on the REPL is processed under the assumption that it is a program, not just data. The crux of my confusion was assuming that data (in the form of a list with no operators) entered into the REPL should be processed/returned as such. – ahron Jan 29 '17 at 17:46
  • page 11: `'(my 3 "Sons")` -> `(my 3 "Sons")` – Rainer Joswig Jan 29 '17 at 17:48
  • Yes. Many many thanks again kind sir. Vielen, vielen Dank. The other thing is d) Parentheses in lisp are truly special, not ordinary delimiters!!!! – ahron Jan 29 '17 at 17:51

2 Answers2

3

The supported Common Lisp syntax for list forms is specified here: CLHS Section 3.1.2.1.2 Conses as Forms.

There are four supported types of lists as forms:

  1. special forms like (quote foo)
  2. macro forms like (defun foo (a1 a1) (+ a1 a2))
  3. function forms like (+ 1 2)
  4. lambda forms like ((lambda (a) (+ a 1)) 2)

That's all. Other lists can't be evaluated in Common Lisp.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
1

You should also note that there is a difference between a list, and an form. All forms (+ 1 2) are lists, but not all lists (2) are forms.

When you type something at the prompt, that needs to be a form. The first part of a form almost always needs to be some kind of operator.

If you have the REPL prompt, and you type in the following, you will get an error, because it is just a list, not a form:

(2)

What will work is something that tells the REPL to construct a list:

'(2)
'(aardvark)

...which is really just shorthand for:

(quote (2))
(quote (aardvark))

Which means that it actually is still a list that starts with an operator, and is therefore a form.

The following examples will return results:

(+ 1 2)
(+ 1 (+ 2 3))

Basically, the way to think about it is that each element (except the first) in the list is evaluated, then the first element is executed on those elements. So (+ 1 (+ 2 3)) is first evaluated as 1 which results in 1, and then (+ 2 3) which again first has the arguments evaluated before the operator is executed, which means 2 and 3 is fed to +, which results in 5, and then 1 and 5 is fed to +.

Is you say (+ 1 (2)), it tries to evaluate each element after the first, going 1 evaluates to 1, but (2) evaluates to... nothing, because the first element is not an operator.

By the way, I find it helpful to look at multiple books and sources, because if one states something in a way I don't understand, I can always consult another one to see if it makes more sense. I suggest these:

Hope that helps!

Community
  • 1
  • 1
Gustav Bertram
  • 14,591
  • 3
  • 40
  • 65
  • 1) `'(aardvark)` is a shorthand for `(quote (aardvark))`, which denotes literal data. It would be unusual to use a backquote for data. A backquote would only be used for a data template. 2) Lisp does not use the word *command*, it's an *operator*. – Rainer Joswig Jan 30 '17 at 16:55
  • Even with a backquote `\`(aardvark)` is not a shorthand for `(list 'aardvark)`, since the latter will always create a new list. – Rainer Joswig Jan 30 '17 at 17:05
  • Ah, thanks @RainerJoswig. I'm still getting the hang of the difference between ticks and backticks. – Gustav Bertram Jan 30 '17 at 18:30