我有一项任务,需要使用两种延迟计算来解释对内存的影响。代码解决了河内问题。
第1类:
(define count-4 (lambda (n) (count-4-helper n (lambda (x) x)))
(define count-4-helper (lambda (n cont)
(if (= n 1)
(cont 1)
(count-4-helper (- n 1) (lambda(res) (cont (+ 1 (* 2 res)))))))) 第2类:
(define count-5 (lambda (n) (count-5-helper n (lambda () 1)))
(define count-5-helper (lambda (n cont)
(if (= n 1)
(cont)
(count-5-helper (- n 1) (lambda() (+ 1 (* 2 (cont)))))))) 第一种情况是延迟计算的经典语法。第二种情况是相同的,只是它没有任何参数,只返回初始值。问题是,这些函数中哪一个是尾递归函数?(我认为两者都是)。他们的内存消耗有多大不同?第二个应该更有效,但我无法真正解释。
耽误您时间,实在对不起。
发布于 2015-03-29 20:20:34
答案是在这两件事上:
(lambda (res) (cont (+ 1 (* 2 res))))
(lambda () (+ 1 (* 2 (cont))))在其中一个而不是另一个中,相对于lambda,在尾位置调用cont。
https://stackoverflow.com/questions/29331911
复制相似问题