sicp每日一题[2.45]
right-split and up-split can be expressed as instances of a general splitting operation. Define a procedure split with the property that evaluating
(define right-split-2 (split beside below))
(define up-split-2 (split below beside))
produces procedures $right-split$ and $up-split$ with the same behaviors as the ones already defined.
这道题本身没什么难度,但是我再次遇到了环境问题。。之前在台式电脑上装的sicp包,用同样的步骤在笔记本上怎么都不行,卸载重装了好几次,最后参考这篇文章终于找到了解决办法。
方法其实也不复杂,随便打开一个窗口,输入#lang planet neil/sicp,然后点击右上角的 run 等待安装完毕重启 DrRacket,再依次点击 Language -> Choose Language,就可以选择看到 SICP(PLaneT 1.18) 了。但是这么简单的方法我用了2个多小时才找到。。
回到这道题,只要注意到 split 的返回值也是一个函数,且它的两个参数分别为 painter 和 n,至于实现,仿照原来的 right-split 或 up-split 即可。
(define wave einstein)
(define (right-split-1 painter n)
(if (= n 0)
painter
(let ((smaller (right-split-1 painter (- n 1))))
(beside painter (below smaller smaller)))))
(define (up-split-1 painter n)
(if (= n 0)
painter
(let ((smaller (up-split-1 painter (- n 1))))
(below painter (beside smaller smaller)))))
(define (split op1 op2)
(define (iter painter n)
(if (= n 0)
painter
(let ((smaller (iter painter (- n 1))))
(op1 painter (op2 smaller smaller)))))
iter)
(define right-split-2 (split beside below))
(define up-split-2 (split below beside))
(paint (right-split-1 wave 1))
(paint (right-split-2 wave 1))
(paint (up-split-1 wave 1))
(paint (up-split-2 wave 1))
效果如下图所示:





浙公网安备 33010602011771号