lua-users home
lua-l archive

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

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.


Fib = Memoise(function(n)
  return Fib(n-1) + Fib(n-2)
Fib[1] = 1
Fib[2] = 1

= Fib[200]

The definition of Memoise I use has been left abandoned on the Lua Wiki
at 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 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 :(