Pointers to freshly minted functions

John Rose rose at think.ARPA
Sat Apr 12 01:18:19 AEST 1986


I have posted to net.sources a small utility which allows creation
of new functions, called ``closures''.  I've already posted this
code, at the end of a lengthy article on closures in Lisp and C,
but I suspect that few people made it to the end of that article.
So, since the topic is still alive, and for the sake of concreteness,
you can look at the code.  Here's an example of what it can do:

typedef double (*PFD)();
extern PFD make_myexp(/* double base */);
/* make_myexp is implemented with malloc, _init_closure,
 * and a `base function' which calls exp. */

PFD Exp, Exp10, Exp2;

/* Each call mallocs a new function object: */
Exp = make_myexp(2.71828183);
Exp10 = make_myexp(10.0);
Exp2 = make_myexp(2.0);

/* Now, pow(x,10.0) == (*Exp10)(x) == Exp10(x) */
-- 
----------------------------------------------------------
John R. Rose		     Thinking Machines Corporation
245 First St., Cambridge, MA  02142    (617) 876-1111 X270
rose at think.arpa				  ihnp4!think!rose



More information about the Comp.lang.c mailing list