笔记 The Little Schemer
MIT Press – The Little Schemer – Daniel P. Friedman.pdf
The Little Schemer – 4th Edition.pdf[20100817 周二 00:40]
[20100905 周日 20:06]* define atom?pp.5 [20100817 周二]
> (define atom?
(lambda (x)
(and (not (pair? x))(not (null? x)))))
> (atom? (quote ()))
#f* cdrpp.13 [20100817 周二]cdr is a list.What is (cdr (cdr l))
where
l is ((b) (x y) ((c)))(((c)))类似的What is the cdr of l
where
l is ( a b c)(b c)* consWhat is the cons of s and l
where s is (banana and)
and
l is (peanut butter and jelly)((banana and ) peanut butter and jelly)* consWhat is (cons s l)
where a is ((a b c))
and
l is bNo answer,
since the second argument l must be a list.* null listWhat is (null? (quote ()))* eqIs (eq? l1 l2) true or false
where l1 is ()
and
l2 is (strawberry)No answer, () and (strawberry) are lists.* eqIs (eq? n1 n2) true or false
where n1 is 6
and
n2 is 7No answer,
6 and 7 are numbers.* eqIs (eq? (cdr l) a) true or false
where
l is (soured milk)
and
a is milkNo answer,
See The laws of Eq? and Cdr.
Young says, (cdr l) is (milk), which is not an atom.* latTrue or false: (lat? l)
where l is ()True,
because it does not contain a list.* recursionInsertR(cons old (cons new (cdr lat)))* recursionpp. 62
在定义multinnsertL中,
需要在递归中改变条件。* recursionpp.81
We recur with a first argument from which we subtract the second
argument. When the function returns, we add 1 to the result.
=> division.* recur with carpp. 89
How are insertR* and rember* similar?They both recur with the car, whenever the car is a list, as well as
with the cdr.* 递归代替迭代pp.94 leftmose的递归调用方法，是用函数的递归调用代替了迭代。
* SexpressionWhat is an Sexpression?An Sexpression is either an atom or a (possibly empty) list of Sexpressions.* lamda
pp.129Now what is
(lambda (a)
(lambda (x)
(eq? x a)))It is a function that, when passed an argument a, returns the function
(lambda (x)
(eq? x a))
where a is just that argument.* lambda applypp.135如何应用函数
(define multiremberf
(lambda (test?)
(lambda (a lat)
…..
)))(multiremberf test?) a lat)multiremberf是这样的函数，以test?作为参数，返回值是一个函数，
返回的函数以 a 和 lat 作为参数。(multiremberf test?) a lat)
相当于
(foo a lat)* lambda应用一个过程
Applying a procedure of lamda返回函数的函数 的应用语法pp.130
Do we need to give a name to eq?saladNo, we may just as well ask
((eq?c x) y)
where
x is salad
and
y is tuna.pp. 129
Now what is
(lamda (a)
(lamda (x)
(eq? x a)))It is a function that, when passed an argument a, returns the function
(lambda (x)
(eq? x a))
where a is jsut that argument.* closurespp. 137开始。递归太复杂。** [http://www.michaelharrison.ws/weblog/?p=34]
:
: (define multirember&co
: (lambda (a lat col)
: (cond
: ((null? lat)
: (col '() '()))
: ((eq? (car lat) a)
: (multirember&co a
: (cdr lat)
: (lambda (newlat seen)
: (col newlat
: (cons (car lat) seen)))))
: (else
: (multirember&co a
: (cdr lat)
: (lambda (newlat seen)
: (col (cons (car lat) newlat)
: seen)))))))
基于closures的解释，参见文献[1]。** 文献[2]提到，可以用Dr.Scheme 语言Pretty Big的debug观察。实验。定义multirember&co以后。Dr.Scheme, Pretty Big.
: >(define foo
: (lambda (x y)
: (length x)))
:
: > (multirember&co 'tuna '(tuna aa bb cc) foo)
: 3** 尝试用drscheme"展开"闭包，不支持；求助刘典，用display显示变量，不够直观，只能显示应用求值，不能显示代换的过程。** 手动: (define multirember&co
: (lambda (a lat col)
: (cond
: ((null? lat)
: (col '() '()))
: ((eq? (car lat) a)
: (multirember&co a
: (cdr lat)
: (lambda (newlat seen)
: (col newlat
: (cons (car lat) seen)))))
: (else
: (multirember&co a
: (cdr lat)
: (lambda (newlat seen)
: (col (cons (car lat) newlat)
: seen)))))))
: (define foo
: (lambda (x y)
: (length x)))
:
: ultirember&co 'tuna '(tuna aa bb cc) foo)substitute:The arguments of calling multirember&co:
:  calling  current lat  col  col, substituted a and
lat  col, substituted lambda 
:  multirember&co   
 
: —————+—————–+—–+—————————+————————
:  1st round  (tuna aa bb cc)  col  /
 lambda (x y)(length x) 
The arguments of calling multirember&co:
:  calling  current lat  col, before substitution
 col, substituted a and lat
 col, substituted lambda

:  multirember&co  



: —————+—————–+———————————————————+———————————————————————+——————————————————————————————
:  2nd round  (tuna aa bb cc)  lambda (newlat seen) (col
newlat (cons (car lat) seen))  lambda (newlat seen) (col newlat (cons
(car '(tuna aa bb cc)) seen))  lambda (newlat seen) ((lambda (x
y)(length x)) newlat (cons (car '(tuna aa bb cc)) seen)) Note:In the expression
: lambda (newlat seen) (col newlat (cons (car lat) seen))
lat is (tuna aa bb cc), insetead of (aa bb cc).
The substitution happens before multirember&co is called,
so the lat is the original one without cdr applying.
Sic passim.End of the note.The col
lambda (newlat seen) ((lambda (x y)(length x)) newlat (cons (car
'(tuna aa bb cc)) seen))
is from:
lambda (newlat seen)
((lambda (x y)(length x))
newlat
(cons (car '(tuna aa bb cc)) seen))The arguments of calling multirember&co:
:  calling  current lat  col, before substitution
 col, substituted a and lat
 col, substituted lambda 
:  multirember&co  

 
: —————+————+———————————————————+—————————————————————–+———————————————————————————————————————————————————
:  3rd round  (aa bb cc)  lambda (newlat seen) (col (cons
(car lat) newlat) seen)  lambda (newlat seen) (col (cons (car '(aa bb
cc)) newlat) seen)  lambda (newlat seen) ((lambda (newlat seen)
((lambda (x y)(length x)) newlat (cons (car '(tuna aa bb cc)) seen)))
(cons (car '(aa bb cc)) newlat) seen) The col
lambda (newlat seen) ((lambda (newlat seen) ((lambda (x y)(length x))
newlat (cons (car '(aa bb cc)) seen))) (cons (car '(bb cc)) newlat)
seen)
is from:
lambda (newlat seen)
((lambda (newlat seen) ((lambda (x y)(length x)) newlat (cons (car
'(aa bb cc)) seen)))
(cons (car '(aa bb cc)) newlat)
seen)The arguments of calling multirember&co:
:  calling  current lat  col, before substitution
 col, substituted a and lat
 col, substituted lambda

:  multirember&co  



: —————+————+———————————————————+————————————————————–+——————————————————————————————————————————————————————————————————————–
:  4th round  (bb cc)  lambda (newlat seen) (col (cons
(car lat) newlat) seen)  lambda (newlat seen) (col (cons (car '(bb
cc)) newlat) seen)  lambda (newlat seen) ((lambda (newlat seen)
((lambda (newlat seen) ((lambda (x y)(length x)) newlat (cons (car
'(tuna aa bb cc)) seen))) (cons (car '(aa bb cc)) newlat) seen)) (cons
(car '(bb cc)) newlat) seen) The col
lambda (newlat seen) ((lambda (newlat seen) ((lambda (newlat seen)
((lambda (x y)(length x)) newlat (cons (car '(tuna aa bb cc)) seen)))
(cons (car '(aa bb cc)) newlat) seen)) (cons (car '(bb cc)) newlat)
seen)
is from:
lambda (newlat seen)
((lambda (newlat seen) ((lambda (newlat seen) ((lambda (x y)(length
x)) newlat (cons (car '(tuna aa bb cc)) seen))) (cons (car '(aa bb
cc)) newlat) seen))
(cons (car '(bb cc)) newlat)
seen)The arguments of calling multirember&co:
:  calling  lat  col, before substitution
 col, substituted a and lat
 col, substituted lambda 
:  multirember&co  for multirember&co 

 
: —————+———————+———————————————————+———————————————————–+————————
:  5th round  (cc)  lambda (newlat seen) (col
(cons (car lat) newlat) seen)  lambda (newlat seen) (col (cons (car
'(cc)) newlat) seen)  
The col
lambda (newlat seen) ((lambda (newlat seen) ((lambda (newlat seen)
((lambda (newlat seen) ((lambda (x y)(length x)) newlat (cons (car
'(tuna aa bb cc)) seen))) (cons (car '(aa bb cc)) newlat) seen)) (cons
(car '(bb cc)) newlat) seen)) (cons (car '(cc)) newlat) seen)
is from:
lambda (newlat seen)
((lambda (newlat seen) ((lambda (newlat seen) ((lambda (newlat seen)
((lambda (x y)(length x)) newlat (cons (car '(tuna aa bb cc)) seen)))
(cons (car '(aa bb cc)) newlat) seen)) (cons (car '(bb cc)) newlat)
seen))
(cons (car '(cc)) newlat)
seen)There is no 6th round of calling multirember&co, because of cond fullfilled.: ((null? lat)
: (col '() '()))Current lat is (cdr '(cc)), as '().The procedure col has been subsitituted in the previous steps to be
: lambda (newlat seen) ((lambda (newlat seen) ((lambda (newlat seen)
((lambda (newlat seen) ((lambda (x y)(length x)) newlat (cons (car
'(tuna aa bb cc)) seen))) (cons (car '(aa bb cc)) newlat) seen)) (cons
(car '(bb cc)) newlat) seen)) (cons (car '(cc)) newlat) seen)Eval (applying '() '() as arguements on col);(col '() '())
=>
((lambda (newlat seen) ((lambda (newlat seen) ((lambda (newlat seen)
((lambda (newlat seen) ((lambda (x y)(length x)) newlat (cons (car
'(tuna aa bb cc)) seen))) (cons (car '(aa bb cc)) newlat) seen)) (cons
(car '(bb cc)) newlat) seen)) (cons (car '(cc)) newlat) seen))
'()
'()
)* 图灵机停机pp. 153
willstop? 无法定义，因为有的函数不会停止。* Ycombinator[http://www.catonmat.net/blog/derivationofycombinator]
解决在函数中应用匿名函数的问题。1. 使用???替换函名函数，如果执行不到，这代码是可以执行的。: (lambda (list)
: (cond
: ((null? list) 0)
: (else
: (add1 (
:
: (lambda (list) ; the
: (cond ;
: ((null? list) 0) ; function
: (else ;
: (add1 (??? (cdr list)))))) ; itself
:
: (cdr list))))))2. 以length为参数以后，下面这段代码需要修正后执行，否则报错 "reference to undefined identifier: ???": ((lambda (length)
: (lambda (list)
: (cond
: ((null? list) 0)
: (else
: (add1 (length (cdr list)))))))
: ???)??? 被改为 length 后可执行。
??? 被改为 '(???) 也可以被执行。
??? 被改为 '() 也可以被执行。
看起来，只要???是个可以被引用的东西就行，但是不能是未定义的。
事实上，lambda (length)的参数不管是什么，都会返回 (lambda (list) 这个函数。如下实验：: (((lambda (length)
: (lambda (list)
: (cond
: ((null? list) 0)
: (else
: (add1 (willnot_run_here (cdr list)))))))
: 'place_holer) '() )下面的代码: ((lambda (f)
: (lambda (list)
: (cond
: ((null? list) 0)
: (else
: (add1 (f (cdr list)))))))
: ((lambda (g)
: (lambda (list)
: (cond
: ((null? list) 0)
: (else
: (add1 (g (cdr list)))))))
: ((lambda (h)
: (lambda (list)
: (cond
: ((null? list) 0)
: (else
: (add1 (h (cdr list)))))))
: ???)))"ince the argument names f, g,h in (lambda (f) …), (lambda (g) …),
(lambda (h) …) are independent, we can rename all of them to length,
to make it look more similar to the length function:"以上并非可以替换名字的原因。
真正的原因是，lamda(f)中的参数f，是lamda(g);
lamda(g)中的参数g，是lambda(h);
而h只是占位符，对程序的求值没有任何作用。
以下代码是上面代码的变形，帮助理解。: (((lambda (lambda_lambda_place_holder)
: (lambda (list)
: (cond
: ((null? list) 0)
: (else
: (add1 (lambda_lambda_place_holder (cdr list)))))))
: ((lambda (lambda_place_holder)
: (lambda (list)
: (cond
: ((null? list) 0)
: (else
: (add1 (lambda_place_holder (cdr list)))))))
: ((lambda (place_holder)
: (lambda (list)
: (cond
: ((null? list) 0)
: (else
: (add1 (willnot_run_here (cdr list)))))))
: 'place_holder))) '(a b))下面的代码中，
把lambda(length)作为参数传给lambda (mklength)
lambda (mklength) 的执行是以参数作为函数，给这个函数的参数是占位符???；
这个参数此时是 lambda (length)。
lambda (length)的占位符参数没有被求值，函数就结束了。
所以，以下代码能求 '(): ((lambda (mklength)
: (mklength ???))
: (lambda (length)
: (lambda (list)
: (cond
: ((null? list) 0)
: (else
: (add1 (length (cdr list))))))))下面的代码中
lambda (mklength)的执行是，
向mklength传递参数 (mklength ???)。
这一参数的解释上面已经提到，执行一次lambda (length)。
执行lambda (length)，最后返回的是 length (cdr list)。
即，以lambda (length)作为lambda (length)的参数。
一共执行两次。: ((lambda (mklength)
: (mklength
: (mklength ???)))
: (lambda (length)
: (lambda (list)
: (cond
: ((null? list) 0)
: (else
: (add1 (length (cdr list))))))))既然???是占位符，不会被求值，所以可以: ((lambda (mklength)
: (mklength mklength))
: (lambda (length)
: (lambda (list)
: (cond
: ((null? list) 0)
: (else
: (add1 (length (cdr list))))))))所有的length改在mklength。
传进来的参数确实是它。
代码略去。然后，length (cdr list) 换成 (mklength mklength) (cdr list)。
这样，(mklength mklength) 递归地返回 原来的lambda (length) 应用在 (cdr list) 上: ((lambda (mklength)
: (mklength mklength))
: (lambda (mklength)
: (lambda (list)
: (cond
: ((null? list) 0)
: (else
: (add1 ((mklength mklength) (cdr list))))))))其实mklenth只执行了一次，然后mklength参数就是占位符了。
但是，"The function works because it keeps adding recursive uses by passing
mklength to itself, just as it is about to expire."递归的跳出条件是(null? list)。把(lambda (length)移出作为lambda (le)的参数，
(lambda (x)作为le的参数。
代码如下。: ((lambda (le)
: ((lambda (mklength)
: (mklength mklength))
: (lambda (mklength)
: (le (lambda (x)
: ((mklength mklength) x))))))
: (lambda (length)
: (lambda (list)
: (cond
: ((null? list) 0)
: (else
: (add1 (length (cdr list))))))))Ycombinator如下。: (define Y
: (lambda (le)
: ((lambda (f) (f f))
: (lambda (f)
: (le (lambda (x) ((f f) x)))))))匿名函数的应用如下。: ((Y (lambda (length)
: (lambda (list)
: (cond
: ((null? list) 0)
: (else
: (add1 (length (cdr list))))))))
: '(a b c d e f g h i j))另一种Ycombinator的写法,
[http://www.mactech.com/articles/mactech/Vol.07/07.05/LambdaCalculus/]
and [http://www.stanford.edu/class/cs242/readings/vocabulary.html]
"When applied to a function, returns its fixed point."
: (define y
: (lambda (f)
: ((lambda (x) (f (x x)))
: (lambda (x) (f (x x))))))参见TLS书(非pdf页码)pp.171，
如何把lambda (length)移出，从而形成了Ycombinatior的框架。*关键：applying arguemnt lambda on precedure lambda, and eval the
augument lambda in precedure lambda.*另，参考[http://blog.vazexqi.com/2006/07/13/ycombinator]原来看不懂的一个原因是，这本电子书有缺页。
[The Little Schemer – 4th Edition.pdf]是个更完整的版本。* valuepp.185
What is (value e)
where
e is (quote nothing)nothing.What is (value e)
where
e is nothing.nothing has no value.* value
Where is (value e)
where
e is ((lambda (nothing)
(cons nothing (quote ())))
(quote
(from nothing comes something)))((from nothing comes something))以 '(from nothing comes something) 作为 lambda(nothing)的参数实验，Pretty Big
>'(nothing)
(nothing)
> (cons '(nothing) '())
((nothing))* interpreterpp. 189
Have we used the table yet?
No, but we will in a moment.Why do we need the table?
To remember the values of identifiers.pp. 188
(define value
(lambda (e)
(meaning e (quote ()))))
(define meaning
(lambda (e table)
((expressiontoaction e) e table)))
* What is the Value of All of This?第10章，略读了。
这一部分应该是用 apply & eval 构造解释器。
准备细读《计算机程序的构造和解释》的相应部分。* laws** carThe primitive car is defined only for nonempty lists.
Young says: the result is a atom.** cdrThe primitive cdr is defined only for nonempty lists.
The cdr of any nonempty list is always another list.** consThe primitive cons takes two arguments.
The second argument to cons must be a list. The result is a list.** null?The primitive null? is defined only for lists.** eq?The primitive eq? takes two arguments.
Each must be a nonnumeric atom.** lat[http://objectmix.com/scheme/186190newbieproblemunderstandinglatatomlittleschemer.html]
I bet, they use lat as short for "List ofAToms".** Sexpression[http://objectmix.com/scheme/186190newbieproblemunderstandinglatatomlittleschemer.html]
The S in Sexpression stands for symbolic. The word Sexpression is
older than Scheme.** The First CommandmentWhen recurring on a list of atoms, lat, ask two questions about it:
(null? lat) and else.
When recurring on a number, n, ask two questions about it: (zero? n) and else.
When recurring on a list of Sexpressions, l, ask thress question
about it: (null? l), (atom? (car l)), and else.
[以下前面的版本]
Always ask null? as the first question in expression any function.When recurring on a list of atoms, lat, ask two questions about it:
(null? lat) and else.
When recurring on a number, n, ask two questions about it: (zero? n) and else.
** The Second CommandmentUse cons to build lists.** The Third CommandmentWhen building a list, describe the first typical element,
and then cons it cons onto the natural recursion.
** The Fourthe CommandmentAlways change at least one argument while recurring.
When recurring on a list of atoms, lat, use (cdr lat).
When recurring on a number, n, use (sub1 n).
And when recurring on a list of Sexpressions, l, use (car l) and (cdr l)
if neither (null? l) nor (atom? (car l)) are true.If must be changed to be closer to termination. The changing argument
must be tested in the termination condition:when useing cdr, test termination with null? and
when using sub1, test termination with zero?.[以下前面的版本]
Always change at least one argument while recurring. It must be
changed to be closer to termination. The changing argument must be
tested in the termination condition:
when using cdr, test termination with null? and
when using sub1, test termination with zero?.** The Fifth CommandmentWhen building a value with +, always use 0 for the value of the
terminating line, for adding 0 does not change the value of an
addition.When building a vluae with X, alwyas use 1 for the value of the
termination line, for multiplying by 1 does not change the value of a
multiplication.When building a vluae with cons, always consider () for the value of
the terminating line.
** The Sixth CommandmentSimplify only after the function is correct.** The Seventh CommandmentRecur on the subparts that are of the same nature:
– On the sublists of a list.
– On the subexpressions of an arithmetic expression.** The Eight CommandmentUse help functions to abstract from representations.** The Ninth CommandmentAbstract common patterns with a new function.** The Tenth CommandmentBuild functions to collect more than one value at a time.* 参考文献[1] [http://www.michaelharrison.ws/weblog/?p=34]Unpacking multirember&co from TLS The purpose of The Little Schemer,
its authors profess, is to teach you to think recursively, and to do
so without presenting too much math or too many computer science
concepts. The book is a ball to read. However, from the perspective of
this reader, who is fairly new to functional programming and totally
new to Scheme, the book gets almost asymptotically more difficult and
complicated towards the end of chapter 8, when we hit the function
multirember&co. Looking around on the web, I noticed quite a few
people had also hit this speed bump and were scratching their heads
about how to go on. I think I can offer some assistance. So, as
threatened yesterday, I now unveil my initial contribution to the wild
world of Lisp, my explication of multirember&co and the concept of
currying. Here's hoping I don't embarrass myself too much.
The Little Schemer, (hereafter "TLS") is the latest iteration of The
Little LISPer, and is presented as a dialogue between teacher and
student. If you take the roll of the student, and try to answer the
teacher's questions, especially those of the form "define this
function whose behavior we've been describing," you can really flex
your neurons. Each chapter is a little more complicated than the
previous, and within each chapter the questions get slightly harder as
you go. It's like walking up a steadily graded hill. Until you get to
page 137 and they hit you with a long function definition, for a
function you've never seen before, and they ask, "What does this do?"Yikes!Here is the code for the function. (Thank you, Geoffrey King, for
transcribing it in your post.)(define multirember&co
(lambda (a lat col)
(cond
((null? lat)
(col '() '()))
((eq? (car lat) a)
(multirember&co a
(cdr lat)
(lambda (newlat seen)
(col newlat
(cons (car lat) seen)))))
(else
(multirember&co a
(cdr lat)
(lambda (newlat seen)
(col (cons (car lat) newlat)
seen)))))))The first clue to dealing with this function is its context. The
previous pages of TLS deal with currying, in which you define a
function like (lambda (x) (lambda(y) (eq? x y) )) — it takes one
argument, parameter x, and then returns the inner function, which also
takes one argument, parameter y. The value you pass as x acts to
customize the inner function by binding the occurance of variable x in
the inner function to the value you passed in. So chapter 8 is about
the practice of wrapping functions in this way.The chapter is also about passing functions as arguments. The first
line of multirember&co, (lambda (a lat col) defines three
parameters. The variables 'a' and 'lat' are by convention used for an
atom and a list of atoms. But 'col' is a function–you have to pass
multirember&co a function that it uses inside its own definition.TLS admits that multirember&co is complicated. "That looks really
complicated!" says the student. But it seeks to simplify the function
by defining functions to stand in for a) the function that will be
passed as 'col'; b) the first inner function defined in the cond
branch (eq? (car lat) a); and c) the inner function defined in the
cond else branch. To try to make you feel better about being up to
your eyelids in deep water, the TLS authors give their functions
friendly names, like "afriend" and "nextfriend." But I prefer names
that tell me what roll the functions play, so here are my renamed
functions:a) the function that will be passed initially as 'col' (and will be
executed last):(define lastfunction
(lambda(x y) (length x)))b) the function called when a matches (car lat):(define whenmatch
(lambda (newlat seen) (col newlat (cons (car lat) seen)))c) the function called when the cond else branch executes:(define whendiffer
(lambda (newlat seen) (col (cons (car lat) newlat) seen))TLS walks you through an execution of multirember&co, and so will
I. To further simplify things, and reduce the amount of typing I have
to do, I'll change the example in the book. Instead of a fourword lat
with some longer words, let's use (berries tuna fish) for our list,
and we'll keep tuna as our atom argument.Here's multirember&co, with the two inner functions replaced by the
predefined helper functions:(define multirember&co
(lambda (a lat col)
(cond
((null? lat)
(col '() '()))
((eq? (car lat) a)
(multirember&co a
(cdr lat)
(whenmatch)))
(else
(multirember&co a
(cdr lat)
(whendiffer))))))When the function is called the first time, a is tuna, lat is (berries
tuna fish), and col is lastfunction. (car lat) is berries, which does
NOT eq tuna, so the else branch executes: multirember&co is called
with tuna as a, (tuna fish) as lat because we pass (cdr lat) and so we
lose the berries atom at the front of the list, and whendiffer as
col.But wait. Actually, we're not just passing the whendiffer function we
defined above. Here is that definition:(lambda (newlat seen) (col (cons (car lat) newlat) seen))This definition contains a variable, lat, that has a value at the time
multirember&co is called recursively: (berries tuna fish). So (car
lat) is (quote berries). What we've got here is a version, or an
instance, of whendiffer that has a value bound to one of its
variables.This is like currying, this binding of values to the variable of a
function and then using this altered function to do something. I think
that currying, however, refers to wrapping functions so that only one
variable at a time is given a value. What this apparent creation of a
specific instance of the function whendiffer DOES have in common with
currying is this: both use closures to encapsulate the instance of the
function with bound variables, or, to be precise, to make a copy of
the function with its own scope that will persist so long as there are
references to the closure. I didn't realize this on my own, of
course. I owe this insight to Richard P. Gabriel's essay The Why of Y,
which you can read in this Dr. Dobb's article or download as a PDF.There's something else in whendiffer that will bind to a value:
col. The function passed, remember, is lastfunction. So we can (and
should) substitute that in for col.Let's give a unique name to the instance (technically the closure) of
the function whendiffer that has these two values bound to it:
whendiffer1. Let's write it out, and set it aside for later use:(define whendiffer1
(lambda (newlat seen) (lastfunction (cons (quote berries) newlat) seen))
)Now, on to iteration two, which we can summarize like this:(multirember&co (quote tuna) (tuna fish) whendiffer1)OK, so this time, (eq? (car lat) a) yields true, and the other branch
of the condexecutes: multirember&co is called with tuna as a, (fish)
as lat, and whenmatch as col. Once again, thanks to currying, the
definition of whenmatch contains expressions to which values are
bound:(car lat), which becomes (quote tuna) , and col, which becomes
whendiffer1. Remember, we just recurred by calling multirember&co
with whendiffer1 as the function argument for the parameter col. So
now let's define the resulting instance of whenmatch as whenmatch1:(define whenmatch1
(lambda (newlat seen) (whendiffer1 newlat (cons (quote tuna) seen)))
)On on to iteration three–we're nearly there–which we can summarize
like this:(multirember&co (quote tuna) (fish) whenmatch1)This time, tuna and fish don't match, which means we're going to recur
with another version of whendiffer, whendiffer2:(define whendiffer2
(lambda (newlat seen) (whenmatch1 (cons (quote fish) newlat) seen))
)Finally, iteration four:
(multirember&co (quote tuna) () whendiffer2)This time lat is an empty list, which means (null? lat) is true, and
the terminating line (col (quote()) (quote())) is executed. Yay! We're
done!Except…The result of the completed execution (col (quote()) (quote())) has to
be evaluated. Here's where everything turns inside out, or rightside
out if you like.First of all, the value of col in the final iteration was
whendiffer2. So we'll start there.(whendiffer2 (quote()) (quote()))Now, look back up and get the definition of whendiffer2 and
substitute it.((lambda (newlat seen) (whenmatch1 (cons (quote
fish) newlat) seen)) (quote()) (quote()))OK, so the parameters newlat and seen both get assigned the value of
an empty list:(whenmatch1 (cons (quote fish) (quote())) (quote()))We can simplify this by consing fish onto the empty list:(whenmatch1 (fish) (quote()))We have a definition for whenmatch1 too. Let's substitute that in now.((lambda (newlat seen) (whendiffer1 newlat (cons (quote tuna)
seen))) (fish) (quote())) )And again assign values, this time (fish) and () to newlat and seen:
(whendiffer1 (fish) (tuna))We're getting somewhere now. Do you see how at each step we're consing
a value onto either seen or newlat? seen has gotten the instance of
tuna, which was the atom we passed to multirember&co at the start,
whereas newlat has gotten the other atom, fish. Guess where berries is
going to go when we get to it.Now, let's substitute our definition of whendiffer1:((lambda (newlat seen) (lastfunction (cons (quote berries) newlat)
seen)) (fish) (tuna))Which becomes….
(lastfunction (berries fish) (tuna) )And now we're back where we started, with lastfunction.( (lambda(x y) (length x)) (berries fish) (tuna) )(length (berries fish) )2So that's how multirember&co works. What does it accomplish? It seems
to separate occurrences of the atom a in the list of atoms lat from
the other atoms in lat, and then it executes lastfunction using the
list of occurrences and the list of other atoms.In an imperative language like C or Java, you would probably define
two variables, one for each list, and then loop through the list of
atoms, testing each element in the list for equality with a, and then
pushing the element onto either of the two lists. Finally, you would
call the final function with the two lists you built.Consider the differences in this approach. Throughout the loop, you
have several variables remaining in scope, which means you have an
opportunity to munge one of them accidentally. Also, how modular is
this hypothetical code? In C, you could pass the lastfunction
function as an argument to a procedure that encapsulates the loop, but
try it in Java. No sir, in Java you'd have to call a method to get the
two lists (which would have to come back wrapped into one object,
probably a String[] array) and then call lastfunction with returnval[
0 ] and returnval[ 1 ]. Not terrible, but not elegant either.That's just scratching the surface, I'm sure. If the example were more
complicated, other implications of the recursive approach might become
clear, at least to smarter people than me. But there is one other
thing to point out.As TLS points out, the function you supply for use with the two lists
is assigned to a parameter names col because "col" stands for
"collector" by convention. What is this function collecting? The two
lists, of course. But more than that each use of col, as it changes
from whendiffer to whenmatch, is persisting the values of the lists
from one step to the next. And that's important because as of page
136, there has been no mention in TLS of an assignment operator. So
even if we wanted to define variables to reference while looping
through the list, we could not. Not yet. After all, such code would
produce what functional programmers refer to, with a sniff, as side
effects.[2] [http://www.rhinocerus.net/forum/langscheme/100568howwhydidtheydo.html]
5 thoughts on “笔记 The Little Schemer”
Leave a Reply
You must be logged in to post a comment.
I like the valuable information you provide to your articles.
I will bookmark your blog and check again here frequently.
I am quite sure I'll be informed plenty of new stuff proper right here! Best of luck for the next!
Also visit my blog post ; Instalación geotermia en Madrid
Hey I know this is off topic but I was wondering if you
knew of any widgets I could add to my blog that automatically tweet my newest
twitter updates. I've been looking for a plugin like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.
Look at my weblog : bacalao
I'm truly enjoying the design and layout of your website. It's a very easy on
the eyes which makes it much more enjoyable for me to come here and visit more often.
Did you hire out a designer to create your theme? Great work!
Here is my blog … natural cellulite treatment
Hey There. I found your blog using msn. This is a really well written article.
I'll be sure to bookmark it and come back to read more of your useful information. Thanks for the post. I'll certainly comeback.
Review my blog post anti cellulite treatment
Hi there! I know this is somewhat off topic but I was wondering if you knew
where I could get a captcha plugin for my comment form?
I'm using the same blog platform as yours and I'm having
trouble finding one? Thanks a lot!
Also visit my web blog :: natural cellulite treatment