[racket] Call by Name

From: Grant Rettke (grettke at acm.org)
Date: Tue Nov 27 14:37:11 EST 2012

In the module where all those functions live, export a collection that
contains references to all of those functions and then iterate over
it:

#lang racket

(provide (all-defined-out))

(define (test1) (display 1))
(define (test2) (display 2))
(define (test3) (display 3))

(define all-functions
  (list test1
        test2
        test3))

In the module where you use it you can do this:

#lang racket

(require "foo.rkt")

(for-each (λ (test) (test)) all-functions)

You can do it dynamically but I think it is too much of a hassle.

On Tue, Nov 27, 2012 at 1:29 PM, Joe Gilray <jgilray at gmail.com> wrote:
> Hi, I have a bunch of functions with known names.  How do I call them?
>
> For example.  If I have functions named "test1", "test2", ... "test999" I'd
> like to call them all:
>
> (define (bigtest)
>   (define base "test")
>   (for ([n (range 1 1000)])
>     (call-by-name (string-append base (number->string n)))))
>
> Thanks,
> -Joe
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>



-- 
Grant Rettke | ACM, AMA, COG, IEEE
grettke at acm.org | http://www.wisdomandwonder.com/
Wisdom begins in wonder.
((λ (x) (x x)) (λ (x) (x x)))


Posted on the users mailing list.