lua-users home
lua-l archive

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

On Fri, 2010-10-29 at 17:44 +0800, Johnson Lin wrote:
> This might be a n00b question, but..
> I know Lua is using strict/eager evaluation, but I found some posts on
> the list, saying that it should be possible to implement a "delay"
> function in plain Lua to imitate lazy evaluation. However It seems
> that I can't find even a single simple example on the web that does
> anything close to this. I'm just curious what approach would you take
> if you were to implement something that can mimic lazy evaluation.
> It's not like memoization techniques or lazy tables. I can't find a
> way to truly make a generic expression or function invocation lazy
> with lazy tables.

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,

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