The little shcemer 这本书通读一遍对 shceme 语法将会有个大致的了解。
本身组织方式也比较有趣,通俗易懂。 强力推荐在SICP 前把它通读一遍,有精力的话做做实验。
1. 首先先引入一个 常用的function ,很多实例都对次进行了引用;
(define atom? (lambda (x) (and (not (pair? x)) (not (null? x)))))
接下来以问答的方式引入问题如下
1.1 list or atom
1. Is it true that this is an atom? atom
Yes because atom is a string of characters beginning with the letter a.
2. Is it true that this is an atom? turkey
Yes, because turkey is a string of characters beginning with a letter.
3. Is it true that this is an atom? 1492
Yes, because 1492 is a string of digits.
4. Is it true that this is an atom? u
Yes, because U is a string ofone character, which is a letter.
5. Is it true that this is an atom
Yes, because *abc$ is a string of characters beginning with a letter or special character other than a left "(" or right ")" parenthesis.
6. Is it true that this is a list? (atom)
Yes, because (atom) is an atom enclosed by parentheses.
7. Is it true that this is a list? (atom turkey or)
Yes, becuase it is a collection of atoms enclosed by parentheses.
8. Is it true that this is a list? (atom turkey) or
No, because there are actually two S-expressions not enclosed by parntheses. The first one is a list containing two atoms, ans the second one is an atom.
9. Is it true that this is a list? ((atom turkey) or )
Yes, because the two S-expressions are now enclosed by parentheses.
10. Is it true that this is an S-expression? xyz
Yes, because all atoms are S-expressions.
11. Is it true that this is an S-expression? (x y z)
Yes, because it is a list.
12. Is it true that this is an S-expression? ((x y) z)
Yes, because all lists are S-expressions.
13. Is it true that this is a list? (how are you doing so far)
Yes, because it is a collection of S-expressions enclosed by parentheses.
14. How many S-expressions are in the list and what are they? (how are you doing so far)
Six, how, are, you, doing, so, and far.
15. Is it true that this is a list? (((how ) are ) ((you) (doing so)) far)
Yes, because it is a collection of S-expressions enclosed by parentheses.
16. How many S-expressions are in the list and what are they? (((how ) are ) ((you) (doing so)) far)
Three, ((how) are), ((you) (doing so)), and far.
17. Is it true that this is a list? ()
Yes, because it contain zero S-expressions enclosed by parentheses. This special S-expression is called the null (or empty) list.
18. Is it true that this is an atom? ()
No, because () is just a list.
19. Is it true that this is a list? ( () () () ())
Yes, because it is a collection of S-expressions enclosed by parentheses.
20. What is the car of l where l is the argument. (a b c)
a, because a is the first atom of this list.
21. what is the car of l where ls is ((a b c) x y z)
(a b c) because ( a b c) is the first S-expression of this non-empty list.
22. What is the car of l where l is hotdog
No answer, You cannot ask fo the car of an atom.
23. What is the car of l where l is ()
No answer. You cannot ask for the car of eth empty list.
1.2 The Law of Car
The primitive car is defined only for non-empty lists.
1. What is the car of l where l is (((hotdogs)) (and) (pickle) relish)
((hotdogs)), read as: "The list of the list of hotdogs." ((hotdogs)) is the first S-expression of l.
2. what is (car l) where l is (((hotdogs)) (and) (pickle) relish)
((hotdogs)), because (car l) is another way to ask fo " the car of the list l."
3. What is (car (car l)) where l is (((hotdogs)) (and))
(hotdogs).
4. What is the cdr of l where l is (a b c) Note: "cdr" is pronounced "could-er."
(b,c), because (b c) is the list of l without (car l).
5. What is the cdr of l where l is ((a b c) x y z)
(x y z)
6. What is the cdr of l where l is (hamburger)
()
7. What is (cdr l) where l is ((x) t r)
(t r), because (cdr l) is just another way to ask for "the cdr of the list l."
8. What is (cdr a) where a is hotdogs
No answer You cannot ask for the cdr of an atom.
9. What is (cdr l) where l is ()
No answer. You cannot ask for the cdr of the null list.
The Law of Cdr
The primitive cdr id defined only for non-empty lists. The cdr of any non-empty list is always another list.
1. What is (car (cdr l) where l is (( b) (x y) (( c)))
(x y), because ((x y) ))c))) is (cdr l), and ( x y) is the car o (cdr l).
2. What is (cdr (cdr l)) where l is (( b) (x y) (( c)))
(((c))), because ((x y)) ((c))) is (cdr l), and (((c))) is the cdr of (cdr l).
3. What is (cdr (car l)) where l is (a (b (c )) d)
No answer. Since (car l) is an atom, and cdr does not take an atom as an argument; see Law of Cdr.
4. What does car take as an argument?
It takes any non-empty list.
5. what does cdr take as an argumetn?
It takes any non-empty list.
6. What is the cons of the atom a and the list l. where a is peanut and l is (butter and jelly)
This can also be written "cons a l".
(peanut butter and jelly) because cons adds an atom to the front of a list.
7. What is the cons of s and l where s is (banana and) and l is (peanut butter and jelly)
((banna and ) peanut butter and jelly), because cons adds any S-expression to the front of a list.
8. What is (cons s l) where s is ((help) this) and l is (is very ((hard ) to learn))
(((help) this ) is very ((hard) to learn)).
9. What does cons take as its atguments?
cons takes two arguments: the first one is any S-expression; the second one is any list.
10. What is (xons s l) where s is (a b (c)) and l is ()
((a b (c))), because () is a list.
11. What is (cons s l) where s is a and l is ()
(a).
12. What is (cons s l) where s is ((a b c)) and l is b.
No answer, since the the second argument l must be a list.
In practice (cons a b) works all values a and b
(car (cons a b)) = a
(cdr (cons a b)) = b
13. what is (cons s l) where s is a and l is b
No answer. Why?
The Law of Cons
The primitive cons takes two arguments. The second argumetn to cons must be a list. The result is a list.
1. What is (cons s (car l)) where s is a and ls is ((b) c d)
(a b). Why?
2. What is (cons s (cdr l)) where s is a and l is ((b) c d)
(a c d). Why?
3. Is it true that the list l is the null list where l is ().
Yes, because it is the list composed of zero S-expressions.
This question can also be written: (null? l).
4. What is (null? (quote ()))
True, because (quote()) is a notation for the null list. L:null
5. Is (null? l) true or false where l is (a b c)
False, because l is a non-empty list.
6. Is (null? a) true or false where a is spaghetti.
No answer, because you cannot ask null? of an atom.
In practise, (null? a) is false for everything, except the empty list.
The Law of Null?
The primitive null? is defined only for lists.
1. Is it true or false that s is an atom where s is Harry.
True, because Harry is a string of characters beginning with a letter.
2. Is (atom? s) true of false where s is harry.
True, because (atom? s) is just another way to ask "Is s is an atom?"
3. Is (atom? s) true or false where s is (Harry had a heap of apples)
False, since s is a list.
4. How many arguments does atom? take and what are they?
It takes one argument. The argument can be any S-expression.
5. Is (atom? (car l)) true or false where l is (Harry had a heap of apples)
True, because (car l) is Harry, and Harry is an atom.
6. Is (atom? (cdr l)) true or false where l is (Harry had a heap of apples)
False
7. Is (atom? (cdr l)) true or false where l is (Harry)
False, because the list () is not an atom.
8. Is (atom? (car (cdr l))) true or false where l is (swing low sweet cherry oat)
True, because (cdr l) is (low sweet cherry oat), and (car (cdr l)) is low, which is an atom.
9. True or false: a1 and a2 are the same atom where a1 is Harry and a2 is Harry
True, because a1 is the atom Harry and a2 is the atom Harry.
10. Is (eq? a1 a2) true or false where a1 is harry and a2 is Harry
True, because (eq? a1 a2) is just another way to ask, "Are a1 and a2 the same non-numeric atom?"
11. Is (eq? a1 a2) true or false where a1 is margarine and a2 is butter.
False since a1 and a2 are different atoms.
12. How many arguments does eq? take and what are they?
It takes two arguments. Both of them must be non-mumeric atoms.
13. Is (eq? l1 l2_) true or false where l1 is () and l2 is (strawberry)
No answer, () and (strawberry) are lists.
In pratice, lists may be auguments of eq?. Two lists are eq? if they are the same list.
14. Is (eq? n1 n2) true or false where n1 is 6 and n2 is 7
No answer, 6 and 7 are numbers.
The Law of Eq?
The primitive eq? takes two arguments. Each must be a non-numeric atom.
1. Is (eq? (car l) a) true or false where l is(Mary had a little lamb chop) and a is Mary.
True, because (car l) is the atom Mary, and the argument a is also the atom Mary.
2. Is (eq? (cdr l) a) true or false where l is (sourced milk) and a is milk
No answer. See The Laws of Eq? and Cdr.
3. Is (eq? (car l) (car (cdrl))) true or false where l is (beans beans we need jelly beans)
True, because it compares the first and second atoms in the list.
Part 2
Do It, Do It Again, and Again, and Again...
1. True or false: (lat? l) where l is (Jack Sprat could eat no chicken fat)
True, because each S-expression in l is an atom.
2. True or false: (lat? l) where l is ((jack) Sprat could eat no chicken fat)
False, since (car l) is a list.
3. True or false: (lat? l) where l is (jack (Sprat could) eat no chicken fat)
False, since one of the S-expression in l is a list.
4. True or false: (lat? l) where l is ()
True, because it doest not contain a list.
5. True or false: a lat is a list of atoms.
True! Every lat is a list of atoms!
6.Write a function lat? using some, but not necessarily all, of the following functions: car cdr cons null? atom? and eq?
You were not expected to be abble to do this yet, because you are still missing some ingredients. Go on to the next question. Good luck.
7. (define lat?
(lambda (l)
(cond
((null? l) #t)
((atom? (car l)) (lat? (cdr)))
(else #f))))
What is the value of (lat? l)
where l is the argument (bacon and eggs)
A: #t The application (lat? l) where l is (bacon and eggs) has the value #t---true---because l is a lat.
8. How do we determine the answer #t for the application (lat? l)
You were not expected to know this one either. The answer is determined by answering the questions asked by lat?
Hint: write down the definition of the function lat? and refer to it for the next group of questions.
9. What is the first question asked by (lat? l)
(null? l) Note: (cond ...) asks questions
(lambda...) creates a function;
(define...) gives it a name;
10. What is the meaning of the cond-line ((null? l) #t) where l is (bacon and eggs)
(null? l) asks if the argument l is the null list. If it is, the value of the application is true. If it is not, we ask the next question. In this case, l is not the null list, so we ask the next question.
11. What is the next question?
(atom? (car l))
12. What is the meaning of the line ((atom? (car l)) (lat? (cdr l))) where l is (bacon and eggs)
(atoms? (car l)) asks if the first S-expression of the list l is an atom. If (car l) is an atom, we want to known if the rest of l is also composed only of atoms. If (car l) is not an atom, we ask the next question. In this case (car l) is an aotm, so the value of the function is the value of (lat? (cdr l)).
13. What is the meaning of (lat? (cdr l))
(lat? (cdr l)) finds out if the rest of the list l is composed only of atoms, by referring to the function lat? , with l becoming the value of (cdr l).
14. Now what is the argument for lat?
()
15. What is the meaning of the line ((null? l) #t) where l is now ()
(null? l) asks if the argument l is the null list. If it is, the value of the application is the value of #t.If not, we ask the next question. In this case, () is the null list. So, the value of the application (lat? l) where l is (bacon and eggs), is #t ----true.
16. Do you remember the question about (lat? l)
Probably not. The application (lat? l) has the value #t if the list l is a list of atoms where l is (bacon and eggs)
17. Can you describe what the function lat? does in your own words?
How are our words: "lat? looks at each S-expression in a list, in turn, and asks if each S-expression is an atom, untik it turns out of S-expressions. If it runs out without encountering a listm the value is #t. If it finds a list, the value is #f---false."
To see how we could arive at a value of "false," consider the next few questions.
18. This is the function lat? again:
(define lat?
(lambda(l)
(cond
((null? l) #t)
((atom? (car l)) (lat? (cdr l)))
(else #f))))
what is the value of (lat? l)
Where l is now (bacon (and eggs))
A: #f, since the list l contains an S-expression that is a list.
19. What is the first question?
(null? l)
20. What is the meaning of the line ((atom? (car l)) (lat? (cdr l))). where l is (bacon (and eggs))
(atom? (car l)) asks if(car l) is an atom. if it is, the value is (lat? (cdr l)). If it is not, we ask the next question. In this case, (car l) is an atom, so we want to check if the rest of the list l is composed only of atoms.
21. What is the meaning of (lat? (cdr l))
(lat? (cdr l)) checks to see if the rest of the list l is composed only of atoms, by referring to lat? with l replaced by (cdr l).
22. What is the meaning of the line ((null? l) #t) where l is now ((and eggs))
(null? l) asks if l is the null list. If it is null, the value is #t. If it is not null, we ask the next question. In this case, l is not null, so move to the next question.
23. What is the next question?
(atom? (car l)).
24. What is the meaning of the line ((atom? (car l)) (lat? (cdr l))) where l is now ((and eggs))
(atom? (car l)) asks if (car l) is an atom. If it is, the value is (lat? (cdr l)). If it is not, we move to the next question. In this case, (car l) is not an atom, so we ask the next question.
25. what is the enxt qusetion?
else.
26. What is the meaning of the question else?
else asks if else if true.
27. Is else true?
Yes, because the quesetion else is alway true.
28. else
of course.
29. why is else the last question?
Because we do not need to ask any more questions.
30. Why do we not need to ask any more questions?
Because a list can be empty, can have an atom in the first position, or can have a list in the first position.
31. What is the meaning of the line (else #f)
else asks if else if true. If else is true---as it always is----then the answer is #f ---false.
32. What is )))
These are the closing or matching parentheses of (cond..., (lambda..., and (define..., which appear at the beginning of a function definition.
33. Can you describe how we determined the value #f for (lat? l) where l is (bacon (and eggs))
Here is one way to say it: "lat? l) looks at each item in its argument to see if it is an aotm. If it runs out of items before it find a list, the value of (lat? l) is #t. If it finds a list, as it did in the example (bacon (and eggs)), the value of (lat? l) is #f."
34. Is (or (null? l1) (atom? l2)) true or false where l1 is () and l2 is (d e f g)
True, because (null? l1) is true where l1 is ().
35. Is (or (null? l1) (null? l2)) true or false where l1 is (a b c) and l2 is ()
True, because (null? l2) is true where l2 is ().
36. Is (or (null? l1) (null? l2)) true or false where l1 is (a b c) and l2 is (atom)
False, beacuse neither (null? l1) nor (null? l2) is true where l1 is (a b c) and l2 is (atom).
37. What does (or ...) do?
(or ...) asks two questions, one a time. If the first one is true it stops and answers true. Otherwise it asks the second question and answers with whatwver the second question answers.
38. Is it true or false that a is a member of lat where a is tea and lat is (coffee tea or milk)
True, because one of the atoms of the lat, (coffee tea or milk) is the same as the atom a ---tea.
39. Is (member? a lat) true or false where a is poached and lat is (fried eggs and scrambled eggs)
False, since a is not one of the atoms of the lat.
40. This is the function member?
(define member?
(lambda (a lat)
(cond
((null? lat) #f)
(else (or (eq? (car lat) a)
(member? a (cdr lat)))))))
What is the value of (member? a lat) where a is meat and lat (mashed potatoes and meat gravy)
#t, because the atom meat is one of the atoms of lat, (mashed potatoes and meat gravy).
41. How do we determine the value #t for the above application?
The value is determined by asking the question about (member? a lat). Hint: Write down the definnition of the function member? and refer to it while you work
on the next group of questions.
42. What is the first question asked by (member? a lat)
(null? lat). This also the first question asked by lat?
The First Commandment
(prelimonary)
Alway aks null? as the first question in expressing any function.
1. What is the meaning of the line ((null? lat) #f) where lat is (meshed potatoes and meat gravy)
(null? lat) asks if lat is the null list. If it is, the value is #f, since the atom meat was not found in lat. If not, we ask the next question. In this case, it is not null, so we ask the next question.
2. What is the next question?
else.
3. Why else the next question?
Because we do not need to ask any more questions.
4. Is else really a qusetion?
Yes, else is a quesstion whose value is always true.
5. Waht is the meaning of the line
(else (or (eq? (car lat) a )
(member? a (cdr lat))))
Now that we know that lat is not null?, we have to find out whether the car of lat is the same atom as a, or whether a is somewhere is the rest of lat
The answer (or (eq? (car lat) a)
(member? a (cdr lat)))
does this.
6. True or false:
(or (eq? (car lat) a)
(member? a (cdr lat)))
where a is meat and lat is (mashed potatoes and meat gravy)
A: We will find out by looking at each question in turn.
7. Is (eq? (car lat) a) true or false where a is meat and alt is (mashed potatoes and meat gravy)
False, because meat is not eq? to mashed, the car of (mashed potatoes and meat gravy).
8. What is the second question of (or...)
member? a (cdr lat)). This refers to the function with the argument lat replaced by (cdr lat).
9. Now what are the arguments of member?
a is meat and lat is now (cdr lat), specifically (potatoes and meat gravy).
10. What is the next question?
(null? lat) Remember The First Commandment.
11. Is (null? lat) true or false where lat is (potatoes and meat gravy)
#f---false
12. What do we do now?
Ask the next question.
13. What is the next question?
else
14. What is the meaning of
(or (eq? (car lat) a)
(member? a (cdr lat)))
A: (or (eq? (car lat) a)
(member? a (cdr lat)))
finds out if a is eq? to the car of lat or if a is a member of the cdr of alt by referring to the function.
15. Is a eq? to the car of lat
No, because a is meat and the car of lat is potatoes.
16. So what do we do next?
We ask (member? a (cdr lat)).
17. Now, what are the arguments of member?
a is meat, and lat is (and meat gravy).
18. What is the next question?
(null? lat)
19. What do we do now?
Ask the next question, since (null? lat) is false.
20. What is the next question?
else
21. What is the value of (or (eq? (car lat) a)
(member? a (cdr lat)))
The value of (member? a (cdr lat)).
22. Why
Because (eq? (car lat) a) is false.
23. What do we do now?
Recur---refer to the function with new arguments.
24. What are the new arguments?
a is meat, and lat is (meat gravy).
25. What is the next question?
(null? lat)
26. What do we do now?
Since (null? lat) is false, ask the next question.
27. What is the next question?
else.
28. What is the value of (or (eq? (car lat) a)
(member? a (cdr lat)))
#t, because (car lat), which is meat, and a, which is meat, are the same atom.
Thereforem (or...) answer with #t.
29. What is the value of the application (member? a lat) where a is meat and lat is (meat gravy)
#t, because we have found that meat is a member of (meat gravy).
30. What is the value of the application (member? a lat) where a is meat and lat is (and meat gravy)
#t, because meat is also a member of the lat (and meat gravy).
31. What is the value of the application (member? a lat) where a is meat and lat is (potatoes and meat gravy)
#t, because meat is also member of the lat (potatoes and meat gravy).
32. What is the value of the application (member? a lat) where a is meat and lat is (mashed potatoes and meat gravy)
#t, because meat is also a member of the lat (mashed potatoes and meat gravy). Of course, this is our original lat.
33. Just to make sure your have it right, let's quickly run through it again. What is the value of (member? a lat) where a is meat and lat is (mashed potatoes and meat gravy)
#t, Hint: Write down the definition of the function member? and its arguments and refer to them as you go through the next group of question.
34. (null? lat)
No, Move to the next line.
35. else
Yes.
36. (or (eq? (car lat) a)
(member? a (cdr lat)))
Perhaps.
37. (eq? (car lat) a)
No, Ask the next question.
38. What next?
Recur with a and (cdr lat) where a is meat and (cdr lat) is (potatoes and meat gravy)
39. (null? lat)
No. Move to the next line.
40. else
Yes. but (eq? (car lat ) a) is false. Recur with a and (cdr lat) where a is meat and (cdr lat) is (and meat gravy).
41. (null? lat)
No. Move to the next line.
42. else
Yes, but (eq? (car lat) a) is false. Recur with a and (cdr lat) where a is meat and (cdr lat) is (meat gravy).
43. (null? lat)
No. Move to the next line.
44. (eq? (car lat) a)
Yes, teh value is #t.
45. (or (eq? (car lat) a)
(member? a (cdr lat)))
#t
46. What is the value of (member? a lat) where a is meat and lat is (meat gravy)
#t.
47. What is the value of (member? a lat) where a is meat and lat is (and met gravy)
#t.
48. What is the value of (member? a lat) where a is meat and lat is (mashed potatoes and meat gravy)
#t.
49. What is the value of (member? a lat) where a is liver and lat is (bagels and lox)
#f.
50. Let's work out why it is #t. What's the first question member? asks?
(null? lat)
51. (null? lat)
No, Move to the next line.
52. else
Yes, but (eq? (car lat) a) is false. Recur with a and (car lat) where a is liver and (cdr lat) is (and lox).
53. (null? lat)
No, move the the next line.
54. else
Yes, but (eq? (car lat) a) is false. Recur with a and (cdr lat) where a is liver and (cdr lat) is (lox).
55. (null? lat)
No. Move to the next line.
56. else
Yes, but (eq? (car lat) a) is still false. Recur with a and (cdr lat) where a is liver and (cdr lat) is ().
57. (null? lat)
yes.
58. What is the value of (member? a lat) where a is liver and lat is ()
#f.
59. What is the value of (or (eqL (car lat) a)
(member? a (cdr lat)))
where a is liver and lat is (lox)
#f.
60. What is value of (member? a lat) where a is liver and lat is (lox)
#f.
61. What is the value of (or (eq? (car lat) a)
(member? a (cdr lat)))
where a is liver
and lat is (and lox)
#f.
62. What is the value of (member? a lat) where a is liver and lat is (and lox)
#f.
63. What is the value of (or (eq? (car lat) a)
(member? a (cdr lat)))
where a is liver and lat is (bagel and lox)
#f.
64. What is the value of (member? a lat) where a is liver and lat is (bagels and lox)
#f.
PART 3 Cons the Magnificent
1. What is (rember a lat) whhere a is mint and lat is (lamb chops and mint jelly)
(lamb chops and jelly) "Rember" stands for remove a member.
2. (rember a lat) where a is mint and lat is (lamb chops and mint flavored mint jelly)
(lamb chops and flavored mint jelly)
3. (rember a lat) where a is mint and lat is (lamb chops and mint flavored mint jelly)
(lamb chops and flavored mint jelly)
4. (rember a lat) where a is cup and lat is (coffee cup tea cup and hick up)
(coffee tea cup and hick cup).
5. What does (rember a lat) do?
It takes an atom and a lat as its arguments, and makes a new lat with the first occurrence of the atom in the old lat removed.
6. What steps should we use to do this?
First we will test (null? lat) ---The First Commandment.
7. And if (null? lat) is true?
return ().
8. What doe we know if (null? lat) is not true?
We know that there must be at least one atom in the lat.
9. Is there any other question we should ask about the lat?
No. Either a lat is empty or it contains at least one atom.
10. What do we do if we know that the lat contains at least one atom?
We ask whether a is equal to (car lat).
11. How do we ask questions?
By using (cond
(__________ __________)
(__________ __________)).
12. How do we ask if a is the same as (car lat)
(eq? (car lat) a).
13. What would be the value of (rember a lat) if a were the same as (car lat)
(cdr lat).
14. What do we do if a is not the same as (car lat)
We want to keep (car lat), but also find out if a is somewhere in the rest of the lat.
15. How do we remove the first occurrence of a in the rest of lat
(rember a (cdr lat)).
16.Is there any other question we should ask?
No.
17. Now, let's write down what we have so far:
(define rember
(lambda (a lat)
(cond
((null? lat) (quote ()))
(else (cond
((eq? (car lat) a) (cdr lat))
(else (rember a
(cdr lat))))))))
What is the value of (rember a lat) where a is bacon and lat is (bacon lettuce and tomato)
A: (lettuce and tomato).
Hint: Write down the function rember and its arguments, and refer to them as you go through the next sequence of questions.
18. Now, let's see if this function works. What is the first question?
(null? lat).
19. What do we do now?
Move to the next line and ask the next question.
20. else
Yes.
21. What next?
Ask the next question.
22. (eq? (car lat) a)
Yes, so the value is (cdr lat). In this case, it the list (lettuce and tomato).
23. Is this the correct value?
Yes, because it is the orginal list without the atom bacon.
24. But did we really use a good example?
Who knows? But the proof of the pudding is in the eating, so let's try another example.
25. What does rember do?
It takes an atom and a lat as its arguments, and makes a new lat with the first occurrence of the atom in the old lat removed.
26. What do we do now?
We compare each atom of the lat with the atom a , and if the comparison fails we build a list that begins with the atom we just compared.
27. What is the value of (rember a lat) where a is and ,and lat is (bacon lettuce and tomato)
(bacon lettuce tomato).
28. Let us see if our function rember works. What is the first question asked by rember
(null? lat)
29. What do we do now ?
Move to the next line, and ask the next question.
30. else
Okay, so ask the next question.
31. (eq? (car lat) a)
No, so move to the next line.
32. What is the meaning of (else (rember a (cdr lat)))
else asks if else is true ---- as it always is --- and the rest of the line says to recur with a and (cdr lat), where a is and and (cdr lat) is
(lattuce and tomato)
33. (null? lat)
No, so move to the next line.
34. else
sure.
35. (eq? (car lat) a)
No, so move to the next line.
36. What is the meaning of (rember a (cdr lat))
Recure where a is and and (cdr lat) is (and tomato).
37. (null? lat)
No, so move to the next line, and ask the next question.
38. else
Of course.
39. (eq? (car lat) a)
Yes.
40. So what is the result?
(cdr lat) -----(tomato)
41. Is this correct?
No, since (tomato) is not the list (bacon lettuce and tomato)
with just a ----and-----removed.
42. What did we do wrong?
We dropped and, but we also lost all the atoms preceding and.
43. How can we keep from losing the atoms bacon and lettuce.
We use Cons the Magnificeng. Remember cons, from chapter 1?
The Second Commandment
Use cons to build lists.
1. Let's see what happens when we use cons
(define rember
(lambda (a lat)
(cond
((null? lat) (quote ()))
(else (cond
((eq? (car lat) a) (cdr lat))
(else (cons (car lat)
(rember a
(cdr lat))))))))
what is the value of (rember a lat)
where a is and
and
lat is (bacon lettuce and tomato)
A: (bacon lettuxe tomato)
Hint: Make a copy of this funtion with cons and the arguments a and lat so you can refer to it for the following questions.
2. What is the first question?
(null? lat).
3. What do we do now?
Ask the next question.
4. else
yes, of course.
5. (eq? (car lat) a)
No, so move to the next line.
6. What is the meaning of
(cons (car lat)
(rember a
(cdr lat)))
where a is and
and lat is (bacon lettuce and tomato)
A: It says to cons the car of lat---bacon---onto the value of (rember a (cdr lat)).
But since we don't know the value of (rember a (cdr lat)) yet, we must find it before we can cons (car lat) onto it.
7. What is the meaning of (rember a (cdr lat))
This refer to the function with lat replaced by (cdr lat) --- ( lettuce and tomato)
8. (null? lat)
No, so move to the next line.
9. else
Yes, ask the next question.
10. (eq? (car lat) a)
No, so move to the next line.
11. What is the meaning of
(cons (car lat)
(rember a
(cdr lat)))
A: It says to cons the car of lat---lettuce---onto the value of (rember a (cdr lat)).
But since we don't know the value of (rember a (cdr lat)) yet, we must find it before we can cons (car lat) onto it.
12. What is the meaning of (rember a (cdr lat))
This refer to the function with lat replaced by (cdr lat) --- (lettuce and tomato).
13. (null? lat)
No, so move to the next line.
14. What is the meaning of
(cons (car lat)
(rember a
(cdr lat)))
It says to cons the car of lat--- lettuce---onto the value of (rember a (cdr lat)).
But since we don't know the value of (rember a (cdr lat)) yet, we must find it before we can cons (car lat) onto it.
15. What is the meaning of (rember a (cdr lat))
This refers to the function with lat replaced by (cdr lat), that is , (and tomato).
16. (null? lat)
No, so ask the next question.
17. else
Still.
18. (eq? (car lat) a)
Yes.
19. What is the value of the line ((eq? (car lat) a) (cdr lat))
(cdr lat)---(tomato).
20. Are we finished?
Certainly not! We know what (remember a lat) is when lat is (and tomato), but wedon't yet know what it is when lat is (lettuce and tomato) or
(bacon lettuce and tomato).
21. We now have a value for (rember a (cdr lat)) where a is and and (cdr lat) is (and tomato) This value is (tomato) What next?
A:Recall that we wanted to cons lettuce onto the value of (rember a (cdr lat))
where a was and and (cdr lat) was (and tomato). Now that we have this value,
which is (tomato), we can cons lettuce onto it.
22. What is the result when we cons lettuce onto (tomato)
(lettuce tomato).
23. What does (lettuce tomato) represent?
A: It represnets the value of
(cons (car lat)
(rember a
(cdr lat))),
when lat is (lettuce and tomato)
and
(rember a (cdr lat)) is (tomato).
24. Are we finished yet?
Not quite. So far we know what (rember a lat) is when lat is (lettuce and tomato), but we don't ye know what it is when lat is (bacon lettuce and tomato).
25. We now have a value for (rember a (cdr lat)) where a is and
and (cdr lat) is (lettuce and tomato) This value is (lettuce tomato) This is not the final value, so what must we do again?
A: Recall that, at one time, we wanted to cons bacon onto the value of (rember a (cdr lat)), where a was and and (cdr lat) was (lettuce and tomato).
Now that we have this value, which is (lettuce tomato), we can cons bacon onto
it.
26. What is the result when we cons bacon onto (lettuce tomato)
(bacon lettuce tomato)
27. What does (bacon lettuce tomato) represent?
It represents the value of (cons (car lat)
(rember a (cdr lat))),
when lat is (bacon lettuce and tomato)
and
(rember a (cdr lat)) is (lettuce tomato).
28. Are we finished yet?
Yes.
29. Can you put in your own words how we determined the final value
(bacon lettuce tomato)
A: In our words:
The function rember checked each atom of the lat, once at a time, to see if it was the same as the atom and. If the car was not the same as the atom, we saved it to be consed to the final value later. When rember found the atom and, it dropped it, and consed the prevous atoms back onto the rest of the lat.
30. Can you rewrite remember so that it reflects the above description?
Yes, we can simplify it.
(define rember
(lambda (a lat)
(cond
((null? lat) (quote()))
((eq? (car lat) a) (cdr lat))
(else (cons (car lat)
(rember a (cdr lat)))))))
31. Do you think this is simpler?
Funtions like rember can always be simplified in this manner.
32. So why don't we simplify right away?
Because the na function's structure does no coincide with its argument's structure.
33. Let's see if the new rember is the same as the old one. What is the value of the application
(rember a lat) where a is and and lat is (bacon lettuce and tomato)
A: (bacon lettuce tomato)
Hint: Write down the function rember and it's arguments and refer to them as you go through
the next sequence of questions.
34. (null? lat)
No.
35. (eq? (car lat) a)
No.
36. else
Yes, so the value is (cons (car lat)
(rember a (cdr lat))).
(cons (car lat)
(rember a (cdr lat)))