Does anyone know the proper code for a Y-combinator in Scheme that takes an additional argument? I tried the following solutions. The non-tail recursive implementation works, however the tail recursive implementaiton fails. Appreciate any guidance here. Thanks
;; Y-combinator (define Y (lambda (f) ((lambda (x) (f (delay (x x)))) (lambda (x) (f (delay (x x))))))) ;Value: y ;; end Y-combinator ;; non-tail recursive, works ((Y (lambda (r) (lambda (x) (if (< x 2) 1 (* x ((force r) (- x 1))))))) 5) ;Value: 120 ;; Tail reclusive implication, fails. ((Y (lambda (r) (lambda (x acc) (if (< x 2) acc (r (- x 1) (* x acc)))))) 5 1) ;The object #[promise 18] is not applicable. ;; Z-combinator. works. (define Z (lambda (f) ((lambda (g) (f (g g))) (lambda (g) (f (lambda args (apply (g g) args))))))) ;Value: z ((Z (lambda (r) (lambda (x) (if (< x 2) 1 (* x (r (- x 1))))))) 5) ;Value: 120 ;; end Z-combinator Nick
_______________________________________________ MIT-Scheme-users mailing list MIT-Scheme-users@gnu.org https://lists.gnu.org/mailman/listinfo/mit-scheme-users