IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 1 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New I've just followed the first step of execution
of this thing using the Lisp evaluation model. I can see it working. My head spins. I still don't understand how it works!
--


- I was involuntarily self-promoted into management.

[link|http://kerneltrap.org/node/4484|Richard Stallman]

New I understand it!
Better yet, I think that I can explain it.

This is all much simpler if we allow ourselves functions of 2 variables. It is fairly easy to build a recursive function of one variable starting with functions of 2 variables:
\nF = proc { |recurse, n|\n  if n == 0 then\n    1\n  else\n    n * recurse.call(recurse, n-1)\n  end\n}\n\nY = proc { |builder, n|\n  builder.call(builder, n)\n}\n\np Y.call(F, 5)  # prints 120\n

The idea being that Y calls F with F as an argument, and that sets up the recursion. You can even write that directly without the temporary variables Y and F just by expanding out Y and F in the expression Y.call(F,5):
\np proc {|builder, n|\n    builder.call(builder, n)\n  }.call(\n    proc { |recurse, n|\n      if n == 0 then\n        1\n      else\n        n * recurse.call(recurse, n-1)\n      end\n    },\n    5\n  )\n

But a brilliant guy named Haskell Curry has a clever trick, you don't need functions of 2 variables if you have functions that return functions. The translation is perfectly mechanical. The idea is that something like:
\nX = proc {|foo, bar|\n  ...\n}\nX.call(this, that)\n

can always be written as
\nX = proc {|foo| proc{ |bar|\n  ...\n}}\nX.call(this).call(that)\n

where ... remains unchanged. So edit the declaration and the calls to functions of 2 variables, and we only need functions of one variable.

Let's apply this mechanical translation to F above. We had:
\nF = proc { |recurse, n|\n  if n == 0 then\n    1\n  else\n    n * recurse.call(recurse, n-1)\n  end\n}\n

Since we now want both recurse and F to be functions of 1 variable, we get the following:
\nF = proc { |recurse| proc { |n|\n  if n == 0 then\n    1\n  else\n    n * recurse.call(recurse).call(n-1)\n  end\n}}\n

And likewise Y becomes:
\nY = proc { |builder| proc { |n|\n  builder.call(builder).call(n)\n}}\n

and our call becomes:
\np Y.call(F).call(5)\n

Or the complex version with no intermediate variables becomes:
\np proc {|builder| proc { |n|\n    builder.call(builder).call(n)\n  }}.call(\n    proc { |recurse| proc { |n|\n      if n == 0 then\n        1\n      else\n        n * recurse.call(recurse).call(n-1)\n      end\n    }}).call(\n    5\n  )\n

Which is, modulo formatting, exactly what Jim wrote.

That runs, but following the execution should make your head spin... :-)

Cheers,
Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
New Read it out loud
Remember the old rhyme/mnemonic for spelling Mississippi when you were six?

"Em - eye - crooked letter - crooked letter - eye ...
crooked letter - crooked letter - eye ...
humpback - humpback - eye."

Now read this:

p proc {|builder| proc { |n|\n    builder.call(builder).call(n)
===

Purveyor of Doc Hope's [link|http://DocHope.com|fresh-baked dog biscuits and pet treats].
[link|http://DocHope.com|http://DocHope.com]
New I detect glazed over eyes
I was going to cement that by writing a version of factorial that just used recursion (without assignment of course!) to reduce everything down to +1, -1. But Ruby lost track of how many braces were open. :-(

There's a moral there somewhere...

Cheers,
Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
New This was as brilliant an explanation as I've ever seen
Thank you.

(not that my head spins any less)
--


- I was involuntarily self-promoted into management.

[link|http://kerneltrap.org/node/4484|Richard Stallman]

     OT: Inner/outer classes. - (pwhysall) - (30)
         Simple explanation: - (admin)
         Wheras an "outer" class is just an ordinary class. -NT - (CRConrad)
         There's some other types as well - (ChrisR)
         Also how Java fakes friendship - (tuberculosis)
         Inner class is declared inside another class - (Arkadiy)
         Thanks all - (pwhysall) - (24)
             Re: Thanks all - (systems) - (23)
                 You misunderstand, on at least one issue: - (CRConrad) - (1)
                     No, they can be used as factories as well. - (admin)
                 s/callbacks/delegates/g - (ChrisR) - (3)
                     Yep, one of the uglier useful things in Java. :-P -NT - (admin)
                     ICLRPD (new thread) - (drewk)
                     Great - another perfectly good term hijacked and hosed (new thread) - (tuberculosis)
                 You can't do #3 - (ben_tilly) - (3)
                     For most uses... - (admin) - (2)
                         I did find that interesting. One major complaint down. - (ben_tilly) - (1)
                             You'll find this interesting too - Bistro - (tuberculosis)
                 They sorta stink as closures - (Arkadiy) - (12)
                     You need Jim Weirich's... - (ChrisR) - (11)
                         Without being rude... - (pwhysall) - (4)
                             A recursive anonymous function... - (ChrisR) - (3)
                                 Is it just interesting, or is it useful? - (pwhysall) - (2)
                                     Well, in Java it's pretty useless. -NT - (ChrisR)
                                     It's a pretty basic feature found in functional languages - (jake123)
                         Re: You need Jim Weirich's... - (JimWeirich) - (5)
                             I've just followed the first step of execution - (Arkadiy) - (4)
                                 I understand it! - (ben_tilly) - (3)
                                     Read it out loud - (drewk) - (1)
                                         I detect glazed over eyes - (ben_tilly)
                                     This was as brilliant an explanation as I've ever seen - (Arkadiy)

OK, my response to that is pending a Google search.
99 ms