A bit of fun with the Y combinator

Zhong Yu zhong.j.yu at gmail.com
Wed Sep 11 09:35:50 PDT 2013


My variation, which is *really* cheating :D

    public static <A,B> Function<A,B> fix2(Function<Function<A,B>,
Function<A,B>> f)
    {
        return new Object()
        {
            Function<A,B> r = a -> f.apply(r).apply(a);
        }.r;
    }

Zhong Yu


On Wed, Sep 11, 2013 at 8:50 AM, Millies, Sebastian
<Sebastian.Millies at softwareag.com> wrote:
> nice. but I think the “simplification” you’re using is cheating.
> You’re not allowed to use “this” references (as you do in the
> interface SelfApplicable).
>
>
> n  Sebastian
>
> From: Samuel Dennis Borlongan [mailto:srborlongan at gmail.com]
> Sent: Wednesday, September 11, 2013 10:40 AM
> To: Millies, Sebastian
> Cc: lambda-dev at openjdk.java.net
> Subject: Re: A bit of fun with the Y combinator
>
> I've made a variant:
> http://rosettacode.org/wiki/Y_combinator#Java
> Please note that Y::main's combinator and fixedPoint variables are actually unneeded; they only serve as a way to use the Y combinator without making the coder's eyes bleed (more).
> [code snipped]
>
> Samuel Dennis R. Borlongan
>
> On Wed, Sep 11, 2013 at 3:56 PM, Millies, Sebastian <Sebastian.Millies at softwareag.com<mailto:Sebastian.Millies at softwareag.com>> wrote:
> Hello there,
>
> many people seem to enjoy coding the Y combinator in Java. There are examples
> in Java 7 (e. g. [1] and [2]) which I decided to try and port to Java 8. The popular
> application is to compute the factorial function.
>
> I’ll show my first solution in a minute. The code really does work: There are no
> variables, no names assigned to any function, only lambda expressions and parameters.
> And yet, we can recursively compute the factorial of arbitrary integers.
>
> Here’s the puzzle for you: I have not been able to “generify” the thing, i. e.
> replace all references to “int” in the Y combinator with a suitable type parameter.
> I would always get type incompatibilities. And remember: No named methods or
> variables are allowed. (e. g. the Java 7 code in [2] violates that condition.)
>
> Note that there are a few casts even in the working solution. Why are they necessary?
> It would be nice to get rid of them.
>
> Best Regards,
> Sebastian
>
> [1] http://www.righto.com/2009/03/y-combinator-in-arc-and-java.html
> [2] https://gist.github.com/jonbodner/2571928
>
> Code follows
> ------- 8< ----------
> [code snipped]
>
>
> Software AG – Sitz/Registered office: Uhlandstraße 12, 64297 Darmstadt, Germany – Registergericht/Commercial register: Darmstadt HRB 1562 - Vorstand/Management Board: Karl-Heinz Streibich (Vorsitzender/Chairman), Dr. Wolfram Jost, Arnd Zinnhardt; - Aufsichtsratsvorsitzender/Chairman of the Supervisory Board: Dr. Andreas Bereczky - http://www.softwareag.com
>
>


More information about the lambda-dev mailing list