From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/88318 Path: news.gmane.org!not-for-mail From: Aditya Mahajan Newsgroups: gmane.comp.tex.context Subject: Re: Lua to Context Table Date: Sat, 2 Aug 2014 00:48:45 -0400 (EDT) Message-ID: References: <278633B4-13E7-48B2-94C3-A49610130862@inradius.net> Reply-To: mailing list for ConTeXt users NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; Format="flowed" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1406954955 19761 80.91.229.3 (2 Aug 2014 04:49:15 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 2 Aug 2014 04:49:15 +0000 (UTC) To: mailing list for ConTeXt users Original-X-From: ntg-context-bounces@ntg.nl Sat Aug 02 06:49:10 2014 Return-path: Envelope-to: gctc-ntg-context-518@m.gmane.org Original-Received: from balder.ntg.nl ([5.39.185.229]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1XDRFk-0007rm-EM for gctc-ntg-context-518@m.gmane.org; Sat, 02 Aug 2014 06:49:08 +0200 Original-Received: from localhost (localhost [127.0.0.1]) by balder.ntg.nl (Postfix) with ESMTP id CE9E910222 for ; Sat, 2 Aug 2014 06:49:07 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at balder.ntg.nl Original-Received: from balder.ntg.nl ([127.0.0.1]) by localhost (balder.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with LMTP id WTMgLx8zcZzG for ; Sat, 2 Aug 2014 06:49:07 +0200 (CEST) Original-Received: from balder.ntg.nl (localhost [IPv6:::1]) by balder.ntg.nl (Postfix) with ESMTP id CB18110223 for ; Sat, 2 Aug 2014 06:49:00 +0200 (CEST) Original-Received: from localhost (localhost [127.0.0.1]) by balder.ntg.nl (Postfix) with ESMTP id 96A90101E8 for ; Sat, 2 Aug 2014 06:48:56 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at balder.ntg.nl Original-Received: from balder.ntg.nl ([127.0.0.1]) by localhost (balder.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with LMTP id jrdEwhQ8H1+P for ; Sat, 2 Aug 2014 06:48:52 +0200 (CEST) Original-Received: from filter4-til.mf.surf.net (filter4-til.mf.surf.net [194.171.167.220]) by balder.ntg.nl (Postfix) with ESMTP id 2C981101E6 for ; Sat, 2 Aug 2014 06:48:52 +0200 (CEST) Original-Received: from tombraider.mr.itd.umich.edu (smtp.mail.umich.edu [141.211.12.86]) by filter4-til.mf.surf.net (8.14.3/8.14.3/Debian-9.4) with ESMTP id s724mmgv012944 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT) for ; Sat, 2 Aug 2014 06:48:50 +0200 Original-Received: FROM adi-thinkpad.local ((null) [135.0.226.162]) By tombraider.mr.itd.umich.edu ID 53DC6DAE.D87B0.6225 ; Authuser adityam; 2 Aug 2014 00:48:46 EDT In-Reply-To: User-Agent: Alpine 2.02 (DEB 1266 2009-07-14) X-Bayes-Prob: 0.0001 (Score 0, tokens from: ntg-context@ntg.nl, base:default, @@RPTN) X-CanIt-Geo: ip=141.211.12.86; country=US; region=Michigan; city=Ann Arbor; latitude=42.2734; longitude=-83.7133; http://maps.google.com/maps?q=42.2734,-83.7133&z=6 X-CanItPRO-Stream: uu:ntg-context@ntg.nl (inherits from uu:default, base:default) X-Canit-Stats-ID: 0VMxEMNdu - 446b4ffb85b4 - 20140802 (trained as not-spam) X-Scanned-By: CanIt (www . roaringpenguin . com) on 194.171.167.220 X-BeenThere: ntg-context@ntg.nl X-Mailman-Version: 2.1.14 Precedence: list List-Id: mailing list for ConTeXt users List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ntg-context-bounces@ntg.nl Original-Sender: ntg-context-bounces@ntg.nl Xref: news.gmane.org gmane.comp.tex.context:88318 Archived-At: On Sat, 2 Aug 2014, luigi scarso wrote: > On Sat, Aug 2, 2014 at 12:59 AM, John Kitzmiller wrote: > >> Here is Lua code that prints the first nine fibonacci numbers: >> >> local function fib(n) >> f={1,1} >> for i=3,9 do >> f[i]=f[i-1]+f[i-2] >> end >> return(f[n]) >> end >> for n=1,9 do print(fib(n)) end >> >> Here is Context+Lua that prints 3 x 3 table of the first nine natural >> numbers: >> >> \starttext >> \setupTABLE[each][each][frame=off,align=left,width=4em,height=2em] >> \startluacode >> context.bTABLE() >> for j = 0, 2 do >> context.bTR() >> for k = 1, 3 do >> context.bTD() >> context(3*j+k) >> context.eTD() >> end >> context.eTR() >> end >> context.eTABLE() >> \stopluacode >> \stoptext >> >> How can these be combined to print a 3 x 3 table of the first nine >> fibonacci numbers? >> >> I have tried a couple of ways to iterate over the lua table f in the most >> indented line of context, looked at what pertinent threads I could find, >> 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) > f={1,1} > for i=3,9 do > f[i]=f[i-1]+f[i-2] > end > return(f[n]) > end > > > \starttext > \setupTABLE[each][each][frame=off,align=left,width=8em,height=2em] > > \startluacode > local function fib(n) > local f={1,1} > for i=3,9 do > f[i]=f[i-1]+f[i-2] > end > return(f[n]) > end > context.bTABLE() > for j = 0, 2 do > context.bTR() > for k = 1, 3 do > context.bTD() > context("fib(%s)=%s" ,3*j+k, fib(3*j+k) ) > context.eTD() > end > context.eTR() > end > context.eTABLE() > \stopluacode > \stoptext This version recomputes all fibnocci numbers from scrach each time. Here is a memoized version: \setupTABLE[each][each][frame=off,align=left,width=8em,height=2em] \starttext \startluacode 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 return fibs[n] end context.bTABLE() for j = 0, 2 do context.bTR() for k = 1, 3 do context.bTD() context("fib(%s)=%s" ,3*j+k, fib(3*j+k) ) context.eTD() end context.eTR() end context.eTABLE() \stopluacode \stoptext Aditya ___________________________________________________________________________________ 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 ___________________________________________________________________________________