lua-users home
lua-l archive

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

Le 21 Sep 2006 à 16:49, Philippe Lhoste a écrit :

Rici Lake wrote:
But mine is still 25% faster, and can be improved even more by memoising: rlake@freeb:~$ time lua511 -l 'Memoise' -e 'local rep = Memoise (function(n) return ("%d"):rep(n) end); for i = 1, 1e5 do local t = {1,2,3,4,5,6,7}; local a = rep[#t]:format(unpack(t)) end'

It was explained in this very list a long time ago, but I can't memorize what memoise is... :-)

Memoise is a higher-order function. It takes a function as an argument and returns a function which is an optimised version. The optimised version works by remembering the already computered answers for inputs that are used more than once. It only works if the function in question is a true function in the strict sense, that is, has no side-effects.

There is an example of this technique in the test/fib.lua script provided in the standard lua distribution (called "cache" in that file).

That leads me to a slightly off-topic field -- but of interest for most programmers, I think.

One question is: is there a good tutorial on functional programming, ideally not relying too much on a given language. I don't have time to re-learn Lisp or to study Scheme.

2 books:

ML for the Working Programmer
Structured Interpretation of Computer Programs, aka SICP.

As it happens they use the languages ML and Scheme, but I think that's an irrelevance. Don't be afraid to learn a new language. SICP is one of my favourite books and is online:: http://

That said, a colleague lent me a copy of Head First Patterns. I have base knowledge of (GoF) patterns, but again, it is not obvious to assimilate them fully (no Borg here).

This brings me back to Lua. Do you think (some of) these patterns can apply to this language? I mean, in its "purest" form, ie. without OO system built above it.

And are they usable in functional programming? Or do this methodology (?) has patterns of its own?

I'm not really a pattern man, but Lua is great for functional programming. You just have to build all the basic tools (map, curry, fold, apply, etc) yourself, but that's fine.