Path: csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!news2.euro.net!postnews2.euro.net!news.wanadoo.nl!not-for-mail From: mhx@iae.nl (Marcel Hendrix) Subject: Re: FORTH Trouble--Please Show Me Newsgroups: comp.lang.forth Message-ID: <87851214938435@frunobulax.edu> Date: Sun, 21 Oct 2012 23:50:48 +0200 References: <7x3917hacl.fsf@ruckus.brouhaha.com> X-Newsreader: iForth 2.0 console (October 21, 2006) Lines: 39 Organization: Wanadoo NNTP-Posting-Date: 21 Oct 2012 21:50:48 GMT NNTP-Posting-Host: s529d937f.adsl.online.nl X-Trace: 1350856248 dr5.euro.net 237 82.157.147.127:55650 X-Complaints-To: abuse@wanadoo.nl Xref: csiph.com comp.lang.forth:16546 Paul Rubin writes Re: FORTH Trouble--Please Show Me [..] > Example in Python: > > def square(x): > return x*x > > def derivative(f, h): # approximate numerical derivative > def df(x): > return (f(x+h) - f(x)) / h > return df > > print square(3) # prints 9 > print derivative(square,0.0001)(3) # prints approximately 6 > You could write something like "derivative" in C with function pointers, > but it would be much messier. [..] I doubt the much in "much messier." -marcel -- ------------------- ANEW -derivative : eval ( ? xt -- ? ) ( F: ? -- ? ) EXECUTE ; : derivative ( xt -- ) ( F: x h -- d ) LOCAL f FLOCALS| h x | x h F+ f eval x f eval F- h F/ ; CR 3e FSQR F. \ prints 9 CR ' FSQR 3e 1e-4 derivative F. \ prints approximately 6 \ FORTH> in idata/derivative \ 9.000000 \ 6.000100 ok