The Little Schemer Chapter 1 – Toys

I’ve been going slower and getting stuck more in Simply Scheme so I decided to take a look at The Little Schemer. I’m trying to find some material to work on the basics a bit more. I’m not currently sure how far I’l carry my The Little Schemer work.

The book says that you have to define atom?, add1 and sub1 to do the exercises, but if i have #lang racket declared at the beginning of my program, add1 and sub1 appear to already work, so I just needed to define atom?

See this StackOverflow answer. Also see this post on getting DrRacket set up.

Somebody made flash cards for this book. I like flash cards, so I’m going to check them out. I also found this github which I haven’t explored yet.

Laws

The book has laws and commandments which it wants you to learn. These are the ones from this chapter:

The Law of Car
The primitive car is defined only for non-empty lists.

The Law of Cdr
The primitive cdr is defined only for non-empty lists. The cdr of any nonempty list is always another list.

The Law of Cons
The primitive cons takes two arguments. The second argument to cons must be a list. The result is a list.

The Law of Null?
The primitive null? is defined only for lists.

The Law of Eq?
The primitive eq? takes two arguments. Each must be a nonnumeric atom.

Selected Exercises

The first set of exercises focuses on identifying things as being atoms (according to the above definition), lists, s-expressions and so on. There’s a prompt on the left and an answer on the right.

I’ll note answers I found interesting or surprising as I go along. I’m not gonna show all of them on here cuz 1) focusing on stuff where you are having problems makes sense and 2) this book isn’t available freely on the web, unlike Simply Scheme, so I want to try to keep my use of it within fair use.


I was unclear on the definition of S-expressions. I went to Wikipedia:

Ah! Now it makes sense that there are two S-expressions in (atom turkey) or.


I thought maybe six but considered that it might be a bit of a trick and they might count the list itself as a seventh S-expression, so i was unsure.


I honestly wasn’t sure on this one. It seems like they count elements of a list as S-expressions but not the elements of sub-lists in a structured list.



Right okay, atoms could make up a list but a list is not an atom.
Note that the empty list is true for (null?) but the empty string is not:



My initial answer was to say the empty list. I think I was thinking of a list (a) instead of the atom a. There is an asymmetry in car and cdr in that from a list l which consists of (a b) you’ll get an atom a if you do (car l), but you’ll get a list (b) if you do (cdr l). I think sometimes I forget that.


Another case where my initial answer was mistaken. For some reason I thought it’d be (peanut (butter and jelly). That’s how list would do it.


This one threw me off. I thought the result would be (a b (c)) for some reason. But if you nest s within a list, it will actually be the case that car of the new list is s and cdr is the empty list l, and that’s the result we want, so yeah.


Another error (i wanted to say (a b). l (a.k.a. b) is not a list!


I said false. The note about “in practice” seems important. In DrRacket, it’s false:

Is this something that varies by implementation of Scheme?


Note that eq? and equal? have different behavior here!


It looks like Racket’s eq? takes numbers: