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))

效果如下图所示:



posted @   再思即可  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示