the little schemer 笔记(1)

第 1 章 玩具

这是原子atom吗?
atom
是的,因为atom是一个字母a开头的字符串。

这是原子atom吗?
turkey
是的,因为atom是字母开头的字符串。

这是原子atom吗?
1492
是的,因为1492是数字的字符串。

这是原子atom吗?
u
是的,因为u是字母开头的字符串,仅仅一个字符。

这是原子atom吗?
*abc$
是的,因为atom是字母或者除了括号”(“和“)”外的特殊字符开头的字符串。

这是list表吗?
(atom)2
是的,因为(atom)是一个atom原子外加括号构成。

这是list表吗?
(atom turkey or)
是的,因为它是由一组原子外加括号构成。

这是list表吗?
(atom turkey) or
不是,这是两个没有括起来的两个S-expression表达式,第一个是含有两个atom原子list列,第二个是一个atom原子。

这是list表吗?
((atom turkey) or)
是的,因为这是两个S-expression表达式外加括号构成。

这是S-expression吗?
xyz
是的,因为所有的atom原子都是S-expression表达式。

这是S-expression吗?
(x y z)
是的,因为它是一个list列表。

这是S-expression吗?
((x y) z)
是的,因为所有的list表都是S-expression表达式。

这是list吗?
(how are you doing so far)
是的,因为它是S-expression表达式外加括号组成的。

list表中有多少个 S-expression?
(how are you doing so far)
六个,how, are, you, doing, so, 和far。

这是list吗?
(((how) are) ((you) (doing so)) far)
是的,因为它是S-expression表达式外加括号组成的。

list表中有多少个 S-expression?
(((how) are) ((you) (doing so)) far)
三个,((how) are), ((you) (doing so)), 和 far

这是list吗?
()
是的,因为它是0个S-expression表达式外加括号组成的。这个特殊的S-expression称为null空(或者empty空)

这是atom原子吗?
()
不是,因为()仅仅是一个list表。

这是list吗?
(() () () ())
是的,因为它是S-expression表达式外加括号组成的。

下面的声明中的表l的car是什么
(a b c)

是a,因为a是list表的第一个atom原子



下面的声明中的表l的car是什么
l是((a b c) x y z)

(a b c), 因为(a b c)是这个非空list表的第一个 S-expression表达式。



下面的声明中的表l的car是什么
l是 hotdog

没有答案,atom原子没有car



下面的声明中的表l的car是什么
l是 ()

没有答案,空表没有car。 注脚:L:nil



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Car的规则
基本操作符car仅为非空list表定义。
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


下面的声明中的表l的car是什么
l是(((hotdogs)) (and) (picle) relish)

((hotdogs)),叫做“hotdogs的列表的列表。”((hotdog))是l的第一个 S-expression.



下面的声明中的表l,取(car l)是什么
l是(((hotdogs)) (and) (picle) relish)

((hotdogs)), (car l)是另一种“l的car”的写法。
示意:
   (car ( ((hotdogs)) (and) (picle) relish))
==>       ((hotdogs))



下面的声明中的表l的(car (car l))是什么
l是(((hotdogs)) (and))

(hotdogs)。示意为:
   (car (car (((hotdogs)) (and))))
==>(car       ((hotdogs))        )
==>            (hotdogs)



下面的声明中的表l的cdr是什么
l是(a b c)

(b c), 因为(b c)是l中除了(car l)的部分。
注意:“cdr”发音为“could-er”。



下面的声明中的表l的cdr是什么
l是((a b c) x y z)

(x y z).




下面的声明中的表l的cdr是什么
l是(hamburger)

()。




下面的声明中的表l,取(cdr l)是什么
l是((x) t r)

(t r),因为(cdr l) 仅仅“表l的cdr“的另一种表示方法。



下面的声明中的表l的cdr是什么
l是 hotdogs

没有答案,atom原子没有cdr。



下面的声明中的表l的cdr是什么
l是()

没有答案,空表null没有cdr。
练习:(null? alpha)除了空表外都为假。


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
cdr的规则
基本操作符cdr仅为非空list表定义。任何非空list的cdr都是另一个list。
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

下面的声明中的表l,取(car (cdr l))是什么
l是((b) (x y) ((x)))

(x y), 因为((x y) ((c)))是(cdr l), (x y) 是(cdr l)的car。示意:
   (car (cdr     ((b)     (x y) ((x)))))
==>(car          (        (x y) ((x)))))
==>                       (x y)



下面的声明中的表l,取(cdr (cdr l)) 是什么
l是((b) (x y) ((x)))

(((x))), 因为((x y) ((c)))是(cdr l),而(x y)是(cdr l)的car。
示意:
   (cdr (cdr ((b)     (x y)     ((x)))))
==>(cdr      (        (x y)     ((x)))))
==>          (                  ((x)) )



下面的声明中的表l,取(cdr (car l))
l是(a (b (c)) d)

没有答案,因为(cdr l)是一个atom原子,它不能取cdr了。


car 的参数可以是什么

任何非空list表。



cdr 的参数可以是什么
任何非空list表。



假设a是 peanut,l是(butter and jelly),那么a和l的cons(这通常写作"(cons a l)",读作把atom原子a cons到list表l上)是什么

(peanut butter and jelly),因为cons把一个atom原子追加到list表最前头。
示意:
   (cons a (butter and jelly)
==>(peanut (butter and jelly)



假设s是(banana and),l是(peanut butter and jelly),那么把scons到l上是什么

((banana and) peanut butter and jelly),因为cons任意 S-expression追加到list表最前头。
示意:
   (cons (banana and)    (peanut butter and jelly) )
==>     ((banana and)     peanut butter and jelly)



假设s是((help) this),l是(is very ((hard) to learn)),那么(cons s l) 是什么

(((help) this) is very ((hard) to learn))。
示意:
   (cons ((help) this)    (is very ((hard) to learn)) )
==>     (((help) this)     is very ((hard) to learn))




cons 的参数可以是什么

cons带两个参数,第一个可以是任何 S-expression,第二个是任意list表。




假设s是 (a b (c), l是(),那么 (cons s l) 是什么

((aa b (c)))



假设s是 a, l是(),那么 (cons s l) 是什么

(a)



假设s是 ((a b c)), l是b,那么 (cons s l) 是什么

没有答案,因为l不是list不能cons。


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
cons的规则

基本操作符cons有两个参数。cons的第二个参数必须是list表,结果返回一个list表。
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;




假设s是a,l是((b) c d),那么(cons s (car l))是什么

(a b)
示意:
   (cons a (car ((b) c d) ))
==>(cons a       (b)       )
==>     (a        b)




假设s是a,l是((b) c d),那么(cons s (cdr l))是什么

(a c d)。
示意:
   (cons a (cdr ((b) c d) ))
==>(cons a      (    c d)  )
==>     (a           c d)




假设l是(),那么l是null list表,对么

是的,因为它是一个含有0个 S-expression表达式的list列表。



(null? (quote ()) 是什么

true真。因为(quote())是空表null的写法。
注:L:空表可以是()或者'(),S:可以是'()。是否是空表,L:null。



假设l是(a b c),那么(null? l)是真还是假

假。L是非空集。



假设A是SPAGHETI,那么(NULL? A)是真还是假

没有答案,ATOM原子没有真假。



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Null的规则

基本操作符null?仅仅为list表定义
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



假如s是harry,那么s是否是atom原子

真。因为harry是字母开始的字符串。




如果 s 是 harrry 那么(atom? s)是什么

真。因为 (atom? s) 是另外一种表达”s是否是一个atom原子“的方式。
注:
L:(defunation? (x)
    (not (listp x)))
S:(define atom?
    (lambda (x)
        (and (not (pair? x)) (not (null? x))))



假如s是 (Harry had a heap of apples),那么 (atom? s) 是真是假

假。因为s是一个list表。



atom?有几个参数

一个。这个参数可以是任意 S-expression。



假如s是 (Harry had a heap of apples),那么 (atom? (car l)) 是真是假

真。
示意:
   (atom? (car (Harry had a heap of apples))
==>(atom?       Harry                      )
==>true



假如s是 (Harry had a heap of apples),那么 (atom? (cdr l)) 是真是假

假。
示意:
   (atom? (cdr (Harry had a heap of apples) ) )
==>(atom?            (had a heap of apples)   )
==>false



假如s是 (swing low sweet cherry oat) ,那么 (atom? (car (cdr l))) 是真是假
真。
示意:
   (atom? (car (cdr (swing low sweet cherry oat))))
==>(atom? (car            (low sweet cherry oat)) )
==>(atom?                  low                 )  )
==>true


假如s是 (swing (low sweet) cherry oat) ,那么 (atom? (car (cdr l))) 是真是假

假。
示意:
   (atom? (car (cdr (swing (low sweet) cherry oat))))
==>(atom? (car            ((low sweet) cherry oat)) )
==>(atom?                  (low sweet)              )
==>false



假如 a1是 Harry,a2是 Harry,那么 a1和a2 是否相等

真。因为a1和a2完全相同。




假如 a1是 Harry,a2是 Harry,那么 (eq? a1 a2) 是真还是假

真。因为 (eq? a1 a2) 是另外一种表达”a1和a2是否是同一个非数值atom原子“的方式。
注:Lisp:eq




假如 a1是 margarine,a2是 butter,那么 (eq? a1 a2) 是真还是假

假。a1和a2不同。



eq?需要多少参数?各是什么?

两个参数,都是非数值atom原子。



假设l1是(),l2是 (strawberry),那么 (eq? l1 l2)是真还是假

没有答案,()和(strawberry)是list表。
注:实际当中是可以作为eq?的参数的。




假设n1是6,n2是7,那么 (eq? n1 n2)是真还是假

没有答案,因为两个参数是数字。
注:实际当中是可以作为eq?的参数的。


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
eq?的规则
基本操作符eq?带有两个参数,两个都是非空数值的atom原子
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;




假设l是 (Mary had a little lamb chop),a是 Mary,那么(eq? (car l) a) 是真还是假

真。




假设l是 (soured milk),a是 Mary,那么(eq? (cdr l) a) 是真还是假

没有答案。



假设l是 (beans beans we need jelly beans),(eq?(car l) (car (cdr l))) 是真还是假

真。因为这是在比较list表中的第一个和第二个atom原子。

==>现在去,做出自己的花生酱果冻三明治<==

 

 

 

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 China Mainland License.

posted @ 2012-08-14 22:03  Z.X.L  阅读(1480)  评论(2编辑  收藏  举报