[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Functional programming (was Re: Default value in function parameters?)
- From: Rici Lake <lua@...>
- Date: Thu, 21 Sep 2006 13:40:29 -0500
On 21-Sep-06, at 1:10 PM, David Given wrote:
Lua doesn't have pattern matching, which is a key point in most
functional languages --- the classic Fibonacci algorithm using pattern
matching is:
fib 1 = 1
fib 2 = 1
fib n = fib (n-1) + fib (n-2)
It could hardly be clearer.
Indeed.
Fib = Memoise(function(n)
return Fib(n-1) + Fib(n-2)
end)
Fib[1] = 1
Fib[2] = 1
= Fib[200]
The definition of Memoise I use has been left abandoned on the Lua Wiki
at http://lua-users.org/wiki/FuncTables since January 17, 2003 :) so I
won't bother repeating it here.
By the way, the above example won't work with values larger than
200 unless you recompile Lua with a larger C recursion limit
(LUAI_MAXCCALLS in luaconf.h)
You might also be interested in the version of lfunclib (look on
http://lua-users.org/wiki/RiciLake for a link); it's oriented
towards making iterators more usable as higher-order objects, but
you might find some functional inspiration, although the
documentation is currently a bit sparse :(
- References:
- RE: Default value in function parameters?, Grellier, Thierry
- Re: Default value in function parameters?, Rici Lake
- Re: Default value in function parameters?, Philippe Lhoste
- Re: Default value in function parameters?, Rici Lake
- Re: Default value in function parameters?, Roberto Ierusalimschy
- Re: Default value in function parameters?, Rici Lake
- Functional programming (was Re: Default value in function parameters?), Philippe Lhoste
- Re: Functional programming (was Re: Default value in function parameters?), David Jones
- Re: Functional programming (was Re: Default value in function parameters?), David Given