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

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

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


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

This worked!  Thanks

On Fri, May 10, 2019 at 10:12 AM Taylor R Campbell <address@hidden> wrote:
> Date: Fri, 10 May 2019 01:28:15 -0400
> From: Nicholas Papadonis <address@hidden>
>
> 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.

Try (force r) instead of r.

reply via email to

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