For Programmers: Free Programming Magazines  


Home > Archive > Scheme > March 2007 > how to emulate "C" point to function in scheme?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to emulate "C" point to function in scheme?
dillo

2007-03-16, 7:10 pm

hi

how to emulate "C" point to function in scheme?
I was trying :
(define pointer-to-forloop
'(define (forloop x x-max dx)
(if (<= x x-max)
(begin
(display x)(display #\space)
(forloop (+ x dx) x-max dx))))
)

But I don't know how to call it...

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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com