lua-users home
lua-l archive

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


>From lua-l@tecgraf.puc-rio.br Thu Jul 29 01:58:27 1999
>From: Dave Bollinger <DBollinger@compuserve.com>

>It seems
>then, if I'm on the right track now, that one of the neat "tricks" with
>upvalues is using a function to return a newly created function which uses
>upvalues - and since it's newly created, the upvalues are "current".  So
>each time you call the "creator" function it could return a different
>function based on the current value of the thing referenced by the upvalue.

Exactly!
Something like this:

 function add(a) retunr function (x) return x+%a end end

 a=add(1)
 b=add(2)
 print(a(10)+a(30))

this prints (10+1)+(30+20)

>   I need to revisit the factorial.lua test code.  I remember going around
>in circles trying to figure that one out!  <grin>  Is this type of "trick"
>what you were intending to demonstrate there?

factorial.lua shows that the standard "functional programming" Y function
is definable in lua. It *is* pretty hard to understand.
I guess there should be a better example in the test directory that uses
upvalues.
--lhf