2

"The Little Schemer":

The Law of Cons
...
The second argument to cons must be a list.

Also, "The Little Schemer":

What is (cons s l)
where s is 'a
and l is 'b

No answer.
Why?

But if I actually do (cons 'a 'b) I get '(a . b) which I know is not a list but it is valid - there is an answer - it is a pair...

joseville
  • 685
  • 3
  • 16
  • 4
    True, the second argument to `cons` can be anything. But they're trying to make your life easier, by simplifying and saying that the second argument is a list, so you can only produce valid lists. – Óscar López Dec 11 '21 at 21:35
  • 2
    `cons` has magic features when it comes to visualization. eg `(cons 1 '(2))` is dislayed `(1 2)` and not `(1 . (2))` which it really is, but if you supply a non list second argument you will always get the dotted formatting. When learning this gets overwelming and very few people learning lisp languages really understands `cons` even after using it for a good while. It's as confusing as pointers in C and very few itroductions to C starts with pointer stuff. – Sylwester Dec 12 '21 at 00:46
  • 2
    If memory serves, The Little Schemer only deals with "proper" lists, and there is a footnote about that, and how "regular" Scheme is different, somewhere in it. – molbdnilo Dec 13 '21 at 13:58
  • @Sylwester funny, first C intro book I read went to pointers straight after the while loops. :) – Will Ness Mar 27 '23 at 20:13

2 Answers2

1

This is common in scheme -- to build sublanguages embedded in scheme.

Friedman builds a sublanguage for his own needs (in the last chapter it builds a full interpreter for his language).

For his purposes he builds his own data structures and each structure has its own axioms. Do not confuse scheme's structures with the structures embedded in scheme he builds. He uses scheme's operators to build his structures, but the axioms of the language he builds are not the same as the axioms of scheme -- scheme is larger. In other lisps or embedded lisps you will find other axioms for the same structures. To make a comparison, the compiler GCC compiles C, it recognizes C as input and it is itself written in C, but the embedded languages it implements at different stages are more complex than C itself (C can be seen embedded in GCC and GCC extends it with many structures (apart from the intermediate languages that are not C) in order to facilitate the writing of the compiler itself).

To understand this better I recommend you to read Anatomy of Lisp -- it is older than SICP, but it's excellent nowadays to understand the foundations.

alinsoar
  • 15,386
  • 4
  • 57
  • 74
0

It does not matter, I have got the same surprise as you when I watch the video https://youtu.be/DrFkf-T-6Co?t=3807 and then read the book But then I realise it doesn't matter at all, that is just the matter of naming things. In sicp lecture, cons glue together 2 things, in TLM cons glue an S-expression into a list. The name is the same but the meaning is different, it does not matter at all

WestMountain
  • 136
  • 1
  • 1
  • 8