Lists vs. Sentences
At this point, you might be confused about the differences between lists and sentences. Keep these simple definitions in mind, and always try to remember what the current problem is dealing with.
A pair is a primitive Scheme type, which keeps track of exactly two values. The two values can be any valid Scheme values. Pairs are constructed using CONS
, and the two values they store are accessed using CAR
and CDR
.
A list is either the empty list ()
or a pair whose cdr
is a list. Note that the car
of the pair can be anything, even another pair or list.
A sentence is a sequence of words. It happens to be implemented as a list whose elements are all words. That is, the car
of each pair making up the sentence is a word. The empty sentence is the same as the empty list ()
.
A word is a symbol, number, or string that works by magic and is a valid argument to any function that normally takes a sentence. However, if a function normally takes a word and a sentence, it now takes a letter and a word, where a letter is a word of length 1.
Sentences | Lists | Result |
---|---|---|
(first '(1 2 3 4)) |
(car '(1 2 3 4)) |
1 |
(butfirst '(1 2 3 4)) |
(cdr '(1 2 3 4)) |
(2 3 4) |
(last '(1 2 3 4)) |
No corresponding method for lists (easy to write) |
4 |
(butlast '(1 2 3 4)) |
No corresponding method for lists (somewhat harder to write) |
(1 2 3) |
(empty? '(1 2 3 4)) |
(null? '(1 2 3 4)) |
#f |
(sentence 1 2 3 4) |
(list 1 2 3 4) |
(1 2 3 4) |
(cons 1 '(2 3 4)) |
||
(append '(1 2) '(3 4)) |
||
No corresponding method for lists (nontrivial but not too bad) |
||
(count '(2 4 1)) |
(length '(2 4 1)) |
3 |
(every square '(1 2)) |
(map square '(1 2)) |
(1 4) |
(keep even? '(1 2)) |
(filter even? '(1 2)) |
(2) |
(member? 1 '(2 3)) |
(member 1 '(2 3)) |
#f |
Be careful about the constructors! While SENTENCE
can take any combination of words and sentences...
CONS
takes exactly two arguments and makes a pair out of them. If you're usingCONS
with lists, make sure the second argument is a list.LIST
takes any number of arguments and makes a list with those arguments as its elements. (Remember, unlike sentences, lists can be nested!) As a way to check yourself, the box-and-pointer diagram for the list returned byLIST
has as many pair-boxes as there are arguments toLIST
, plus whatever is needed to represent each argument.APPEND
takes any number of lists as arguments and attaches them all together (i.e. if you wrote all the lists next to each other and smashed any ")(" that you see). Don't pass words (symbols or numbers) toAPPEND
!
Also, note a small difference between MEMBER?
and MEMBER
. While the former returns #t
if it finds a matching item, the latter returns the pair whose car
is the matching item. (Since anything that isn't #f
counts as true, you can still use this in IF
or COND
tests.) Both procedures return #f
if it can't find the item.
And finally, try very much to only use sentence procedures on words and sentences, and only use list procedures on lists.
Back to Jordy's home page | Course home page