还是那句话,经典不妨一看再看
reference这章的视频可以在edx上看,也可以点开youtube的公开课链接看
这里的总结是假设你都看过视频了( ̄<  ̄)>
这里我们用练习题有一题叫做spinning-bears.rkt过一遍知识点
solution链接下载
spinning-bears-solution.rkt
运行效果
在self-reference中我们有举过ListOfNumber的例子
(@htdd ListOfNumber)
;; ListOfNumber is one of:
;; - empty
;; - (cons Number ListOfNumber)
;; interp. arbitrarily long list of arbitrary numbers
这是list +number(primary data)
如果是list + compound data尼?
spinning-bears就是一个例子
(图中黄色字体是comment, 蓝色字体是template,箭头在comment 和 template有不同的名字
注意箭头的终点都在comemnt 和 template的第一行)
话箭头的过程是当你看到如这句comemnt的时候
;;(cons Bear ListOfBear)
好奇的问了一下, Bear是个啥? 它来自那里? 然后尼找到了出处,并顺便画了个箭头,这个找出处
的过程就叫reference
然后ListOfBear又是个啥? 它来自那里? 然后尼找到了出处,发现居然是它自己,感慨之余, 也顺
便画了个箭头。这个找出处找到自己的过程叫self reference
在template也是同样的意思,但画出来的箭头换个名字
接着我们看一下ListOfBear htdf, 我们看一下spin-bears
(@htdf spin-bears)
(@signature ListOfBear -> ListOfBear)
...
(@template ListOfBear)
(define (spin-bears lob)
(cond [(empty? lob) empty]
[else
(cons (spin-bear (first lob))
(spin-bears (rest lob)))]))
;;=========================================
(@htdf spin-bear)
(@signature Bear -> Bear)
....
(@template Bear)
(define (spin-bear b)
(make-bear (bear-x b)
(bear-y b)
(+ (bear-r b) SPEED)))
运算过程(例子)
(spin-bears (cons (make-bear 0 0 0) (cons (make-bear 1 1 1) empty)))
=(cons (spin-bear (make-bear 0 0 0))
(spin-bears(cons (make-bear 1 1 1) empty)))
=(cons (make-bear 0 0 3)
(spin-bears (cons (make-bear 1 1 1) empty)))
=(cons (make-bear 0 0 3)
(cons (spin-bear (make-bear 1 1 1))
(spin-bears empty)))
=(cons (make-bear 0 0 3)
(cons (make-bear 1 1 4)
(spin-bears empty)))
=(cons (make-bear 0 0 3)
(cons (make-bear 1 1 4)
empty))
最后,有一个问题是可否把spin-bears和 spin-bear和到一起写尼?
像这样子
(define (spin-bears lob)
(cond [(empty? lob) empty]
[else
(cons (make-bear (bear-x (first lob))
(bear-y (first lob))
(+ (bear-r (first lob)) SPEED))
(spin-bears (rest lob)))]))
但这样子,就没有follow 整个reference的template,
对于reference的bear 一定要由bear 的htdf(helper)来
处理. 不follow的话,写出来也0分