mit-scheme-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[MIT-Scheme-users] Y-combinator in Scheme, using promise? Error.


From: Nicholas Papadonis
Subject: [MIT-Scheme-users] Y-combinator in Scheme, using promise? Error.
Date: Fri, 10 May 2019 01:28:15 -0400

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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]