0

I'm studying SICP and at the beginning of section 2.2.2 it gives the following code: (cons '(list 1 2) (list 3 4))) and says it constructs a list like ((1 2) 3 4). But when I typed it into DrRacket(I'm using Racket here actually) it produces '((list 1 2) 3 4) and if I write (cons (list 1 2) (list 3 4)) then it'll be alright. I know in Scheme '(1 2) is equal to (list 1 2) but what does '(list 1 2) mean?

Leo
  • 492
  • 1
  • 5
  • 15

4 Answers4

3

It should mean "a list consisting of the atom list, the atom 1, and the atom 2". Until Scheme evaluates the list (which the single quote prevents), it doesn't treat "list" differently from any other string.

A. L. Flanagan
  • 1,162
  • 8
  • 22
2

Scheme has a convenient syntax for representing data literals: prefix any expression with ' (single quote) and the expression, rather than being evaluated, will be returned as data

For more informations:

http://courses.cs.washington.edu/courses/cse341/04wi/lectures/14-scheme-quote.html

ratnim
  • 129
  • 7
2

Fix output style

First off, When you use the #!racket language in DrRacket, the default way of printing is not printing it's representation but an expression that evaluates to the same. You can turn it off from the menu language >> choose language. You select Show details and under Output style you select write

After pressing Run, when evaluating 'test you will get the output test.

Typo in expression

In section 2.2.2 there is an expression (cons (list 1 2) (list 3 4)). It is not the same as what you wrote in the question, (cons '(list 1 2) (list 3 4)). While an expression (list 1 2) applies the procedure list with values 1 and 2 and thus becomes (1 2), the expression '(list 1 2) just return the quoted data (list 1 2) unchanged.

Thus:

(cons (list 1 2) (list 3 4))   ; ==> ((1 2) 3 4)
(cons '(list 1 2) (list 3 4))  ; ==> ((list 1 2) 3 4)
'(cons '(list 1 2) (list 3 4)) ; ==> (cons '(list 1 2) (list 3 4))
Sylwester
  • 47,942
  • 4
  • 47
  • 79
  • Really? But on my book it truly has an extra ' ... So you want to say adding an extra ' is useless? – Leo Aug 26 '15 at 00:29
  • @Caesar If you follow the link to 2.2.2 you can clearly see that the web edition hasn't quoted the `list` form. The extra `'` changes how the code is interpreted. `(+ 1 2 3)` evaluates to `6` while `'(+ 1 2 3)` evaluates to `(+ 1 2 3)` (a list consisting of the symbol `+` and the numbers 1,2, and 3. Same happens with `(list 1 2 3)` except `list` does something else while the result of `'(list 1 2 3)` is the identical result as `'(+ 1 2 3)` except the first symbol is different. Without quote the first procedure in the expression is different. – Sylwester Aug 26 '15 at 09:49
1

The notation 'foo makes a symbol named foo.

The notation '(foo bar) makes a list with two symbols named foo and bar.

In the same way '(list foo bar) makes a list of three symbols. The symbol 'list happens to be called list.

Now (list 'foo 'bar) makes a list of two symbols called foo and bar.

soegaard
  • 30,661
  • 4
  • 57
  • 106
  • Why `'(foo bar)` is correct but `(list foo bar)` is wrong? Both are making a list with 2 unknown identifier. – Leo Aug 25 '15 at 16:12
  • In `(list foo bar)` the system 1) looks up what `list` is and finds the list constructor 2) looks up `foo` and finds out it is undefined. – soegaard Aug 25 '15 at 16:14
  • But in `'(foo bar)` foo seems also undefined. – Leo Aug 25 '15 at 16:15
  • The `'` means construct a value that prints as `(foo bar)`. The system changes `'(foo bar)` to `(list 'foo 'bar)`. – soegaard Aug 25 '15 at 16:16
  • Can I say `'(foo bar)` just declares a list variable which consists of two other variables foo and bar, but not yet binding to a value? – Leo Aug 25 '15 at 16:23
  • No. `'(foo bar)` creates a list with two elements. The first element is the symbol `'foo` and the second element is the symbol `'bar`. Symbols aren't variables. Symbols are more-or-less the same as strings. – soegaard Aug 25 '15 at 16:24
  • Umm...I can understand now but symbols seem of little use. – Leo Aug 25 '15 at 16:27
  • The difference between symbols and strings is that it is very fast to compare strings to see if they are equal. See the answer to this question for a full explanation: http://stackoverflow.com/questions/8846628/what-exactly-is-a-symbol-in-lisp-scheme – soegaard Aug 25 '15 at 16:29