Tamas K Papp <tkpapp@gmail.com> wrote:
+---------------
| And of course you can have closures in any language if you fight hard
| for it, eg you can implement CL in C and then "have" closures in the
| latter. Thinking about it, reimplementing CL in C is a much cleaner
| way to get closures than strugging with the brain-dead newLISP.
+---------------
Yup, viz.:
$ cat ctest.c
typedef unsigned long lispobj; /* Or whatever your generic type is */
typedef struct closure_s {
lispobj (*func)(lispobj *, ...);
lispobj *env;
} closure_t, *closure_p;
#define funcall_closure(closure, args...) \
(((closure)->func)((closure)->env, ## args))
#define CFUNCP lispobj (*)(lispobj *, ...)
lispobj
foo(lispobj *env, lispobj x, lispobj y)
{
return (env[0] * x) + (env[1] * y);
}
main()
{
lispobj e[2] = {3, 17}; /* Create a small environment */
closure_t c = {(CFUNCP)&foo, &e[0]}; /* Close FOO over it. */
printf("%d\n", funcall_closure(&c, 1, 1000));
}
$ make ctest
cc -O -pipe ctest.c -o ctest
$ ./ctest
17003
$
I've been playing these sorts of games for years, with great success! ;-}
+---------------
| The mere suggestion that one should emulate closures
| by fiddling with lambda forms as lists reeks.
+---------------
Indeed!
-Rob
-----
Rob Warnock <rpw3@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607