[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Scheme's "delay" function in plain Lua?
- From: Johnson Lin <arch.jslin@...>
- Date: Fri, 29 Oct 2010 18:57:14 +0800
On Fri, Oct 29, 2010 at 6:04 PM, Michal Kottman <k0mpjut0r@gmail.com> wrote:
>
> I'm not sure if I'm correct, but shouldn't it be possible to use an
> anonymous function without parameters, whose body consists of only a
> return with the expression you'd like to lazy-evaluate? In order to
> retrieve the value later, you just have to call the function.
>
> For example (I can't think of anything more sophisticated right now,
> sorry):
>
> local a = 1
> local b = 2
> -- make 'c' a lazy-expression of a + b
> local c = function() return a + b end
> ...
> -- now you want to force (SICP terminology) the value of c
> print(c())
>
I'd hoped to use an interface similar to this:
local c = delay(a+b) -- however this is quite not possible without
language support
so instead I was trying:
local c = delay("a+b") -- maybe we can do some magic on the string
or even
local c = delay("+", a, b) -- etc...
I was wondering if Y-combinator can actually be implement in Lua this way,
local Y = function(f)
return (function(x) return f( x(x) ) end) (function(x) return f ( x(x) ) end)
end
As you can see, the x(x) will always resolve to infinite recursion without lazy,
so yes, I have to expand that part into another lambda:
x(x) --> becomes --> function(y) return (x(x)) (y) end
but what if there's a way to write
delay(x(x)) or something like delay("call", x, x) ?
Any suggestions or corrections are welcomed : )