lua-users home
lua-l archive

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


me ~= coroutines -- :)

-Andrew

On Mon, Apr 21, 2014 at 11:14 PM, Sean Conner <sean@conman.org> wrote:
> It was thus said that the Great Dirk Laurie once stated:
>> 2014-04-21 23:33 GMT+02:00 Sean Conner <sean@conman.org>:
>> > It was thus said that the Great Coroutines once stated:
>> >>
>> >> I always remember that local function f() ... end is equivalent to
>> >> local f; f = function () ... end because when I have to write
>> >> recursive functions it errors when it tries to call the
>> >> not-yet-declared identifier from within itself.
>>
>> s/errors/would otherwise error/
>>
>> >   You should try using the Y combinator.
>> >
>> > function Y(f)
>> >   local function g(...) return f(g,...) end
>> >   return g
>> > end
>> >
>> > print(Y(function(rec, x)
>> >           if x < 2 then
>> >             return 1
>> >           else
>> >             return x * rec(x-1)
>> >           end
>> >         end)(5))
>> >
>> >   -spc (Unfortunately, do declare Y with your conding standard requres the
>> >         use of Y ... )
>>
>> I don't understand the very last point made, even after correcting two typos.
>
>   You originally stated:
>
>> I have stopped using "local function f()" because I cannot remember
>> whether it is the same as "local f = function()" or "local f;
>> f=function()". So whichever of those I need, I code explicitly.
>
>   Coroutines was stating that
>
>         local function f() ... end
>
> is the same as:
>
>         local f
>         f = function() ... end
>
> because otherwise, you couldn't write recursive functions if
>
>         local function f() ... end
>
> was equivalent to:
>
>         local f = function() ... end
>
>   In a way, you could think of
>
>         function draw(x1,y1,x2,y3) ... end
>
> as doing
>
>         draw = -- some place holding value
>         draw = function(x1,y1,x2,y2)
>
> which also means that
>
>         local function draw(x1,y1,x2,y2) ... end
>
> follows that pattern:
>
>         local draw = -- some place holding value
>         draw = function(x1,y1,x2,y2) ... end
>
>   The whole bit I added with the Y combinator was just a riff on the
> conversation.  You can think of the Y combinator as allowing a anonymous
> (non-named) function to call itself (the actual description uses some obtuse
> mathematical terms like "fixed point"---I don't pretend to understand what
> exactly it means).
>
>   -spc (I hope that cleared that up ... )
>
>