[racket] Call by Name

From: J. Ian Johnson (ianj at ccs.neu.edu)
Date: Tue Nov 27 14:39:07 EST 2012

If you don't need to dispatch on name at runtime, you can write a macro for this and use format-id to construct the identifier naming the function. Otherwise you would need to use eval, which is highly inadvisable.

#lang racket
(require (for-syntax racket/syntax))
(define-for-syntax ((call-fns-with-base base start end) stx)
  (syntax-case stx ()
    [(_) #`(begin #,@(for/list ([n (range start end)])
                       (with-syntax ([name (format-id base "~a~a" base n)])
                         #`(name))))]))
(define-syntax call-fns (call-fns-with-base #'test 1 1000))
(call-fns)

;(untested)
-Ian
----- Original Message -----
From: "Joe Gilray" <jgilray at gmail.com>
To: "Racket mailing list" <users at racket-lang.org>
Sent: Tuesday, November 27, 2012 2:29:05 PM GMT -05:00 US/Canada Eastern
Subject: [racket] Call by Name


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

Posted on the users mailing list.