| Dave Vandervies 2007-03-16, 7:10 pm |
| In article <1174069116.405667.310780@n76g2000hsh.googlegroups.com>,
dillo <dillogimp@yahoo.com> wrote:
>hi
>
>how to emulate "C" point to function in scheme?
You don't need to; Scheme functions are first-class objects, so you can
pass them around directly instead of using a function.
--------
;Equivalent C code:
;/*func is a pointer to function*/
;int call_with_arg( void (*func)(int), int arg)
;{return func(arg);}
;
(define call-with-arg
;func is the function to call
(lambda (func arg)
(func arg)))
;A function to call
(define add-42
(lambda (n)
(+ n 42)))
;Equivalent C code:
;void print_42_more(int n)
;{ printf("%d\n",call_with_arg(add_42,n)); }
(define print-42-more
(lambda (n)
(display (call-with-arg add-42 n))
(newline)))
;Equivalent C code for this one would require rewriting the anonymous
; function as a named function somewhere in the code
(define print-17-more
(lambda (n)
(display (call-with-arg
(lambda (x) (+ x 17))
n))
(newline)))
--------
dave
--
Dave Vandervies dj3vande@csclub.uwaterloo.ca
>No? Then I should find something else or (how horrible!) have some work done.
WORK??? I think that would be overreacting.
--Peter Pichler and Richard Heathfield in comp.lang.c
|