(define (gcd a b remainder-count)
(if(= b 0)
(begin (display "\nremainder count: ")
(display remainder-count)
(display "\n gcd result:")
(display a)
(display "\n")
a)
(gcd b (remainder a b) (+ remainder-count 1))))
(gcd 12 9 0)
;(gcd 12 28 0)
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
(define (gcd-new-if a b remainder-count)
(new-if (= b 0)
(begin (display "\nremainder count: ")
(display remainder-count)
(display "\n gcd result:")
(display a)
(display "\n")
a
)
(gcd-new-if b (remainder a b) (+ remainder-count 1))))
(gcd-new-if 12 9 0)
;(new-if (> 1 2) (begin (display "1") (display "1") )(display "2"))
以上gcd函数自动记录remainder调用次数。(gcd 12 9 0)调用remainder次数为2次。
if的scheme运算是正则运算,不管怎么样子都是先对谓词求值,然后分支运算。
要使得scheme运算为应用序,则需要使用new-if,自己定义个函数来代替if,由于new-if是自定义的函数,则使用应用序先对其参数顺序求值。因此会导致gcd-new-if 循环调用而产生死循环,并且由于remainder的存在,在循环调用的过程中,remainder a b 的参数b为0时,则发生错误,即求余运算中的除数为0,导致错误。
分享到:
相关推荐
西普 我的 SICP 练习。
NULL 博文链接:https://pengpeng.iteye.com/blog/1344689
SICP习题解答,主要第一章的内容习题答案
sicp-clojure 在 Clojure 中解决的 SICP 练习
SICP 习题答案 计算机程序的构造和解释 1-3章 习题答案
sicp 我对SICP练习的回答
SICP-解决方案我对 SICP 练习的回答
SICP 在 Scheme 中制定的 SICP 练习
sicp
sicp in python 中文版 sicp in python 中文版 sicp in python 中文版 !!!download>>>https://github.com/wizardforcel/sicp-py-zh
SICP书中练习的解决方案。 使用mit-scheme编译器9.2。
SICP中文第二版SICP中文第二版SICP中文第二版SICP中文第二版SICP中文第二版
SICP - 笔记和练习我把它放在这里是因为有一天它可能会帮助某人。 练习是ex*文件。 章节中的注释和代码是ch文件。安装下载 Racket.app。 使用 DrRacket.app 或像这样启动 Racket repl: /Applications/Racket\ v...
SICP-Python版本
SICP 使用的scheme解释器 以前叫DrScheme
sicp 2.2.4节图形语言的racket程序包,配置路径,C:\Users\Administrator\AppData\Roaming\Racket
MySICP解决方案 我试图在这个寒假里学习旱灾,这是我对课本习题的解答... 这些答案不一定正确,我可能无法解决所有练习。 但是您可以参考提供的标准解决方案我正在使用中文版的sicp。 教科书中有一些错误, 可以在找到
SICP 解题集