Recursion 两种延迟计算的比较

Recursion 两种延迟计算的比较,recursion,lambda,scheme,racket,Recursion,Lambda,Scheme,Racket,我有一个作业,我需要解释使用两种延迟计算对内存的影响。代码解决了河内问题 类型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 c

我有一个作业,我需要解释使用两种延迟计算对内存的影响。代码解决了河内问题

类型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)))))))) 
第一种情况是延迟计算的经典语法。第二种情况相同,只是它不获取任何参数,只返回初始值。 问题是哪一个函数是尾部递归的?我认为它们都是。他们的内存消耗有多大不同?第二种方法应该更有效,但我真的无法解释


谢谢您的时间。

答案在以下两个lambda中:

(lambda (res) (cont (+ 1 (* 2 res))))

(lambda () (+ 1 (* 2 (cont))))

在其中一种情况下,而不是在另一种情况下,cont被称为相对于lambda的尾部位置。

谢谢!我没有注意订单。