> On Aug 2, 2014, Aditya Mahajan <adit...@umich.edu> wrote: >> On Sat, 2 Aug 2014, luigi scarso wrote: >>> On Sat, Aug 2, 2014, John Kitzmiller <k...@inradius.net> wrote: >>> Here is Lua code that prints the first nine fibonacci numbers: >>> >>> local function fib(n) >>> f={1,1} >>> ...the Wiki entries, CLD, and tried Aditya's method from Stack Exchange; I >>> am >>> missing something. >>> >> >> You have a *global* table f that collides with the internals of context >> ... >> local function fib(n) >> local f={1,1} >> ... > > This version recomputes all fibnocci numbers from scrach each time. Here > is a memoized version: > > local fibs = {1, 1} > local function fib(n) > if fibs[n] == nil then > print(">>>>", "Computing fib:" .. n) > fibs[n] = fib(n-1) + fib(n-2) > end > ...
Thanks to you both! I missed the global/local conflict, and then learned a way to avoid double computation; thanks again! John ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________