Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Extract from Jigsaw

Extract from Jigsaw

Scheduled Pinned Locked Moved Clever Code
csharplinqfunctionalquestion
18 Posts 16 Posters 11 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Pascal Ganaye

    This is an extract from the 'Implementing Programming Languages using C# 4.0' article published last week (late oct 2011) Some of the code really blow my mind. This code works, but how on earth does it work? I need to spend spend some time on this.

        public delegate Lambda Lambda(Lambda x);
    
        public static Lambda Id         = x => x;
        public static Lambda Zero       = f => x => x;
        public static Lambda True       = x => y => x;
        public static Lambda False      = x => y => y;
        public static Lambda One        = f => x => f(x);
        public static Lambda Two        = f => x => f(f(x));
        public static Lambda Succ       = n => f => x => f(n(f)(x));
        public static Lambda Three      = Succ(Two);
        public static Lambda Pred       = n => f => x => n(g => h => h(g(f)))(u => x)(Id);
        public static Lambda Plus       = m => n => f => x => m(f)(n(f)(x));
        public static Lambda Sub        = m => n => n (Pred) (m);
        public static Lambda And        = p => q => p(q)(p);
        public static Lambda Or         = p => q => p(p)(q);
        public static Lambda Not        = p => a => b => p(b)(a);
        public static Lambda IfThenElse = p => a => b => p(a)(b);
        public static Lambda IsZero     = n => n(x => False)(True);
        public static Lambda IsLtEqOne  = n => IsZero(Pred(n));
        public static Lambda Pair       = x => y => f => f(x)(y);
        public static Lambda First      = pair => pair(True);
        public static Lambda Second     = pair => pair(False);
        public static Lambda Nil        = x => True;
        public static Lambda Null       = p => p(x => y => False);
        public static Lambda LtEq       = x => y => IsZero(Sub(x)(y));
        public static Lambda Gt         = x => y => LtEq(y)(x);
        public static Lambda Eq         = x => y => And(LtEq(x)(y))(LtEq(y)(x));
    
    L Offline
    L Offline
    leppie
    wrote on last edited by
    #5

    Pascal Ganaye wrote:

    public delegate Lambda Lambda(Lambda x);

    Would be cooler if it was written as:

    delegate λ λ(λ x);

    Which is perfectly legal in C# :)

    IronScheme
    ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

    F 1 Reply Last reply
    0
    • P Pascal Ganaye

      This is an extract from the 'Implementing Programming Languages using C# 4.0' article published last week (late oct 2011) Some of the code really blow my mind. This code works, but how on earth does it work? I need to spend spend some time on this.

          public delegate Lambda Lambda(Lambda x);
      
          public static Lambda Id         = x => x;
          public static Lambda Zero       = f => x => x;
          public static Lambda True       = x => y => x;
          public static Lambda False      = x => y => y;
          public static Lambda One        = f => x => f(x);
          public static Lambda Two        = f => x => f(f(x));
          public static Lambda Succ       = n => f => x => f(n(f)(x));
          public static Lambda Three      = Succ(Two);
          public static Lambda Pred       = n => f => x => n(g => h => h(g(f)))(u => x)(Id);
          public static Lambda Plus       = m => n => f => x => m(f)(n(f)(x));
          public static Lambda Sub        = m => n => n (Pred) (m);
          public static Lambda And        = p => q => p(q)(p);
          public static Lambda Or         = p => q => p(p)(q);
          public static Lambda Not        = p => a => b => p(b)(a);
          public static Lambda IfThenElse = p => a => b => p(a)(b);
          public static Lambda IsZero     = n => n(x => False)(True);
          public static Lambda IsLtEqOne  = n => IsZero(Pred(n));
          public static Lambda Pair       = x => y => f => f(x)(y);
          public static Lambda First      = pair => pair(True);
          public static Lambda Second     = pair => pair(False);
          public static Lambda Nil        = x => True;
          public static Lambda Null       = p => p(x => y => False);
          public static Lambda LtEq       = x => y => IsZero(Sub(x)(y));
          public static Lambda Gt         = x => y => LtEq(y)(x);
          public static Lambda Eq         = x => y => And(LtEq(x)(y))(LtEq(y)(x));
      
      A Offline
      A Offline
      Addy Tas
      wrote on last edited by
      #6

      When i try to read through this my brain starts to reboot. I think i first have to upload a new instructionset before trying to do this again. on the side; the nice thing of it is that the app won't process it untill the result is needed. So far for the "it compiles therefore it works"

      Cogito ergo sum

      1 Reply Last reply
      0
      • P Pascal Ganaye

        This is an extract from the 'Implementing Programming Languages using C# 4.0' article published last week (late oct 2011) Some of the code really blow my mind. This code works, but how on earth does it work? I need to spend spend some time on this.

            public delegate Lambda Lambda(Lambda x);
        
            public static Lambda Id         = x => x;
            public static Lambda Zero       = f => x => x;
            public static Lambda True       = x => y => x;
            public static Lambda False      = x => y => y;
            public static Lambda One        = f => x => f(x);
            public static Lambda Two        = f => x => f(f(x));
            public static Lambda Succ       = n => f => x => f(n(f)(x));
            public static Lambda Three      = Succ(Two);
            public static Lambda Pred       = n => f => x => n(g => h => h(g(f)))(u => x)(Id);
            public static Lambda Plus       = m => n => f => x => m(f)(n(f)(x));
            public static Lambda Sub        = m => n => n (Pred) (m);
            public static Lambda And        = p => q => p(q)(p);
            public static Lambda Or         = p => q => p(p)(q);
            public static Lambda Not        = p => a => b => p(b)(a);
            public static Lambda IfThenElse = p => a => b => p(a)(b);
            public static Lambda IsZero     = n => n(x => False)(True);
            public static Lambda IsLtEqOne  = n => IsZero(Pred(n));
            public static Lambda Pair       = x => y => f => f(x)(y);
            public static Lambda First      = pair => pair(True);
            public static Lambda Second     = pair => pair(False);
            public static Lambda Nil        = x => True;
            public static Lambda Null       = p => p(x => y => False);
            public static Lambda LtEq       = x => y => IsZero(Sub(x)(y));
            public static Lambda Gt         = x => y => LtEq(y)(x);
            public static Lambda Eq         = x => y => And(LtEq(x)(y))(LtEq(y)(x));
        
        B Offline
        B Offline
        b10543748
        wrote on last edited by
        #7

        hmm.. is like http://en.wikipedia.org/wiki/Brainfuck[^] --Edit-- I miss VB6 :doh: :doh: :( :( :( ;P

        1 Reply Last reply
        0
        • P Pascal Ganaye

          This is an extract from the 'Implementing Programming Languages using C# 4.0' article published last week (late oct 2011) Some of the code really blow my mind. This code works, but how on earth does it work? I need to spend spend some time on this.

              public delegate Lambda Lambda(Lambda x);
          
              public static Lambda Id         = x => x;
              public static Lambda Zero       = f => x => x;
              public static Lambda True       = x => y => x;
              public static Lambda False      = x => y => y;
              public static Lambda One        = f => x => f(x);
              public static Lambda Two        = f => x => f(f(x));
              public static Lambda Succ       = n => f => x => f(n(f)(x));
              public static Lambda Three      = Succ(Two);
              public static Lambda Pred       = n => f => x => n(g => h => h(g(f)))(u => x)(Id);
              public static Lambda Plus       = m => n => f => x => m(f)(n(f)(x));
              public static Lambda Sub        = m => n => n (Pred) (m);
              public static Lambda And        = p => q => p(q)(p);
              public static Lambda Or         = p => q => p(p)(q);
              public static Lambda Not        = p => a => b => p(b)(a);
              public static Lambda IfThenElse = p => a => b => p(a)(b);
              public static Lambda IsZero     = n => n(x => False)(True);
              public static Lambda IsLtEqOne  = n => IsZero(Pred(n));
              public static Lambda Pair       = x => y => f => f(x)(y);
              public static Lambda First      = pair => pair(True);
              public static Lambda Second     = pair => pair(False);
              public static Lambda Nil        = x => True;
              public static Lambda Null       = p => p(x => y => False);
              public static Lambda LtEq       = x => y => IsZero(Sub(x)(y));
              public static Lambda Gt         = x => y => LtEq(y)(x);
              public static Lambda Eq         = x => y => And(LtEq(x)(y))(LtEq(y)(x));
          
          M Offline
          M Offline
          Mike Poz
          wrote on last edited by
          #8

          Quote:

          public delegate Lambda Lambda(Lambda x);

          Obviously this was written by a tri-lam nerd during the "Revenge" period of movies... :)

          Mike Poz

          1 Reply Last reply
          0
          • P Pascal Ganaye

            This is an extract from the 'Implementing Programming Languages using C# 4.0' article published last week (late oct 2011) Some of the code really blow my mind. This code works, but how on earth does it work? I need to spend spend some time on this.

                public delegate Lambda Lambda(Lambda x);
            
                public static Lambda Id         = x => x;
                public static Lambda Zero       = f => x => x;
                public static Lambda True       = x => y => x;
                public static Lambda False      = x => y => y;
                public static Lambda One        = f => x => f(x);
                public static Lambda Two        = f => x => f(f(x));
                public static Lambda Succ       = n => f => x => f(n(f)(x));
                public static Lambda Three      = Succ(Two);
                public static Lambda Pred       = n => f => x => n(g => h => h(g(f)))(u => x)(Id);
                public static Lambda Plus       = m => n => f => x => m(f)(n(f)(x));
                public static Lambda Sub        = m => n => n (Pred) (m);
                public static Lambda And        = p => q => p(q)(p);
                public static Lambda Or         = p => q => p(p)(q);
                public static Lambda Not        = p => a => b => p(b)(a);
                public static Lambda IfThenElse = p => a => b => p(a)(b);
                public static Lambda IsZero     = n => n(x => False)(True);
                public static Lambda IsLtEqOne  = n => IsZero(Pred(n));
                public static Lambda Pair       = x => y => f => f(x)(y);
                public static Lambda First      = pair => pair(True);
                public static Lambda Second     = pair => pair(False);
                public static Lambda Nil        = x => True;
                public static Lambda Null       = p => p(x => y => False);
                public static Lambda LtEq       = x => y => IsZero(Sub(x)(y));
                public static Lambda Gt         = x => y => LtEq(y)(x);
                public static Lambda Eq         = x => y => And(LtEq(x)(y))(LtEq(y)(x));
            
            J Offline
            J Offline
            jschell
            wrote on last edited by
            #9

            Pascal Ganaye wrote:

            This code works, but how on earth does it work?

            There are many ways to create code that "works". From that set there are a limited number which are easily maintainable. Myself I prefer to create code that works and which is easily maintainable. It takes time to do that just as it takes time to create 'clever' code. However the former is something that actually has something to do with what I get paid for where the latter does not.

            K 1 Reply Last reply
            0
            • D DaveAuld

              Pascal Ganaye wrote:

              but how on earth does it work?

              I think there was a liberal sprinkling of pixie dust applied. :omg:

              Dave Find Me On: Web|Facebook|Twitter|LinkedIn


              Folding Stats: Team CodeProject

              M Offline
              M Offline
              Matthew Dennis
              wrote on last edited by
              #10

              I think there was a liberal amount of pixie dust snorted.

              1 Reply Last reply
              0
              • J jschell

                Pascal Ganaye wrote:

                This code works, but how on earth does it work?

                There are many ways to create code that "works". From that set there are a limited number which are easily maintainable. Myself I prefer to create code that works and which is easily maintainable. It takes time to do that just as it takes time to create 'clever' code. However the former is something that actually has something to do with what I get paid for where the latter does not.

                K Offline
                K Offline
                KP Lee
                wrote on last edited by
                #11

                Amen. Worse, this is intentionally written to be confusing and nearly unreadable. Which kinda goes with the Lambda function design to begin with.

                1 Reply Last reply
                0
                • P Pascal Ganaye

                  This is an extract from the 'Implementing Programming Languages using C# 4.0' article published last week (late oct 2011) Some of the code really blow my mind. This code works, but how on earth does it work? I need to spend spend some time on this.

                      public delegate Lambda Lambda(Lambda x);
                  
                      public static Lambda Id         = x => x;
                      public static Lambda Zero       = f => x => x;
                      public static Lambda True       = x => y => x;
                      public static Lambda False      = x => y => y;
                      public static Lambda One        = f => x => f(x);
                      public static Lambda Two        = f => x => f(f(x));
                      public static Lambda Succ       = n => f => x => f(n(f)(x));
                      public static Lambda Three      = Succ(Two);
                      public static Lambda Pred       = n => f => x => n(g => h => h(g(f)))(u => x)(Id);
                      public static Lambda Plus       = m => n => f => x => m(f)(n(f)(x));
                      public static Lambda Sub        = m => n => n (Pred) (m);
                      public static Lambda And        = p => q => p(q)(p);
                      public static Lambda Or         = p => q => p(p)(q);
                      public static Lambda Not        = p => a => b => p(b)(a);
                      public static Lambda IfThenElse = p => a => b => p(a)(b);
                      public static Lambda IsZero     = n => n(x => False)(True);
                      public static Lambda IsLtEqOne  = n => IsZero(Pred(n));
                      public static Lambda Pair       = x => y => f => f(x)(y);
                      public static Lambda First      = pair => pair(True);
                      public static Lambda Second     = pair => pair(False);
                      public static Lambda Nil        = x => True;
                      public static Lambda Null       = p => p(x => y => False);
                      public static Lambda LtEq       = x => y => IsZero(Sub(x)(y));
                      public static Lambda Gt         = x => y => LtEq(y)(x);
                      public static Lambda Eq         = x => y => And(LtEq(x)(y))(LtEq(y)(x));
                  
                  M Offline
                  M Offline
                  Mel Padden
                  wrote on last edited by
                  #12

                  Looks like mutton dressed as... no, no, I'm not going to finish that. Nice snippet. Little workout for the neurons.

                  Smokie, this is not 'Nam. This is bowling. There are rules. http://melpadden.wordpress.com LinkedIn

                  1 Reply Last reply
                  0
                  • P Pascal Ganaye

                    This is an extract from the 'Implementing Programming Languages using C# 4.0' article published last week (late oct 2011) Some of the code really blow my mind. This code works, but how on earth does it work? I need to spend spend some time on this.

                        public delegate Lambda Lambda(Lambda x);
                    
                        public static Lambda Id         = x => x;
                        public static Lambda Zero       = f => x => x;
                        public static Lambda True       = x => y => x;
                        public static Lambda False      = x => y => y;
                        public static Lambda One        = f => x => f(x);
                        public static Lambda Two        = f => x => f(f(x));
                        public static Lambda Succ       = n => f => x => f(n(f)(x));
                        public static Lambda Three      = Succ(Two);
                        public static Lambda Pred       = n => f => x => n(g => h => h(g(f)))(u => x)(Id);
                        public static Lambda Plus       = m => n => f => x => m(f)(n(f)(x));
                        public static Lambda Sub        = m => n => n (Pred) (m);
                        public static Lambda And        = p => q => p(q)(p);
                        public static Lambda Or         = p => q => p(p)(q);
                        public static Lambda Not        = p => a => b => p(b)(a);
                        public static Lambda IfThenElse = p => a => b => p(a)(b);
                        public static Lambda IsZero     = n => n(x => False)(True);
                        public static Lambda IsLtEqOne  = n => IsZero(Pred(n));
                        public static Lambda Pair       = x => y => f => f(x)(y);
                        public static Lambda First      = pair => pair(True);
                        public static Lambda Second     = pair => pair(False);
                        public static Lambda Nil        = x => True;
                        public static Lambda Null       = p => p(x => y => False);
                        public static Lambda LtEq       = x => y => IsZero(Sub(x)(y));
                        public static Lambda Gt         = x => y => LtEq(y)(x);
                        public static Lambda Eq         = x => y => And(LtEq(x)(y))(LtEq(y)(x));
                    
                    B Offline
                    B Offline
                    BillWoodruff
                    wrote on last edited by
                    #13

                    Please complete the following analogy: RegEx is to text-processing ... as C# Lambda is to ____________ ? thanks, Bill

                    Scipio: "That's true, Berganza; and what makes the miracle greater is, that we not only speak, but hold intelligent discourse, as though we had souls capable of reason; whereas we are so far from having it, that the difference between brutes and man consists in this, that man is a rational animal, and the brute is irrational." Cervantes, "Colloquy of Dogs," 1613CE. The two talking dogs, Scipio, Berganza, are hallucinations in the mind of a soldier with plague fever undergoing a "sweating" cure.

                    1 Reply Last reply
                    0
                    • L leppie

                      Pascal Ganaye wrote:

                      public delegate Lambda Lambda(Lambda x);

                      Would be cooler if it was written as:

                      delegate λ λ(λ x);

                      Which is perfectly legal in C# :)

                      IronScheme
                      ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                      F Offline
                      F Offline
                      flurbius
                      wrote on last edited by
                      #14

                      How do you type a λ in visual studio ?

                      L 1 Reply Last reply
                      0
                      • F flurbius

                        How do you type a λ in visual studio ?

                        L Offline
                        L Offline
                        leppie
                        wrote on last edited by
                        #15

                        I can't tell you. My lame DELL laptop does not have a numpad and numpad emulation... Try one of these methods: http://www.fileformat.info/tip/microsoft/enter_unicode.htm[^] Or the ugly version \u03bb

                        IronScheme
                        ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                        G 1 Reply Last reply
                        0
                        • L leppie

                          I can't tell you. My lame DELL laptop does not have a numpad and numpad emulation... Try one of these methods: http://www.fileformat.info/tip/microsoft/enter_unicode.htm[^] Or the ugly version \u03bb

                          IronScheme
                          ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                          G Offline
                          G Offline
                          Gary R Wheeler
                          wrote on last edited by
                          #16

                          Has everyone forgotten charmap?

                          Software Zen: delete this;

                          1 Reply Last reply
                          0
                          • P Pascal Ganaye

                            This is an extract from the 'Implementing Programming Languages using C# 4.0' article published last week (late oct 2011) Some of the code really blow my mind. This code works, but how on earth does it work? I need to spend spend some time on this.

                                public delegate Lambda Lambda(Lambda x);
                            
                                public static Lambda Id         = x => x;
                                public static Lambda Zero       = f => x => x;
                                public static Lambda True       = x => y => x;
                                public static Lambda False      = x => y => y;
                                public static Lambda One        = f => x => f(x);
                                public static Lambda Two        = f => x => f(f(x));
                                public static Lambda Succ       = n => f => x => f(n(f)(x));
                                public static Lambda Three      = Succ(Two);
                                public static Lambda Pred       = n => f => x => n(g => h => h(g(f)))(u => x)(Id);
                                public static Lambda Plus       = m => n => f => x => m(f)(n(f)(x));
                                public static Lambda Sub        = m => n => n (Pred) (m);
                                public static Lambda And        = p => q => p(q)(p);
                                public static Lambda Or         = p => q => p(p)(q);
                                public static Lambda Not        = p => a => b => p(b)(a);
                                public static Lambda IfThenElse = p => a => b => p(a)(b);
                                public static Lambda IsZero     = n => n(x => False)(True);
                                public static Lambda IsLtEqOne  = n => IsZero(Pred(n));
                                public static Lambda Pair       = x => y => f => f(x)(y);
                                public static Lambda First      = pair => pair(True);
                                public static Lambda Second     = pair => pair(False);
                                public static Lambda Nil        = x => True;
                                public static Lambda Null       = p => p(x => y => False);
                                public static Lambda LtEq       = x => y => IsZero(Sub(x)(y));
                                public static Lambda Gt         = x => y => LtEq(y)(x);
                                public static Lambda Eq         = x => y => And(LtEq(x)(y))(LtEq(y)(x));
                            
                            L Offline
                            L Offline
                            Lutoslaw
                            wrote on last edited by
                            #17

                            ++geek;

                            Greetings - Jacek

                            1 Reply Last reply
                            0
                            • B BobJanova

                              That hurts my brain.

                              A Offline
                              A Offline
                              Addy Tas
                              wrote on last edited by
                              #18

                              Lucky you, i have just experienced a personal BSoD... My mind has just rebooted and is doing a memory test.... Lots of bad sectors found :sigh:

                              Cogito ergo sum

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups