[plt-scheme] Programatically Constructed Symbol Used As An Identifier???
----- Original Message -----
From: "Grant Rettke" <grettke at acm.org>
To: "Zelah Hutchinson" <zelah at inbox.com>
Cc: <plt-scheme at list.cs.brown.edu>
Sent: Friday, June 26, 2009 4:26 PM
Subject: Re: [plt-scheme] Programatically Constructed Symbol Used As An Identifier???
> Hi Zelah,
>
> On Fri, Jun 26, 2009 at 6:31 AM, Zelah Hutchinson<zelah at inbox.com> wrote:
>> The "add" function is where I run in to trouble. The problem seems to be that 'adder is not the same as
>> the function "adder".
>
>> (symbol? 'adder)
> #t
>> (procedure? 'adder)
> #f
>> (symbol? adder)
> #f
>> (procedure? adder)
> #t
>
>> How can I make this work the way I want it to. The important parts for me are that the function I wish to
>> call is already defined somewhere in my program and for calling it I wish to construct the existing name
>> at run-time.
>
> (define (add x y)
> (apply (symbol->function (append-symbol 'add 'er))
> (list x y)))
>
> (define (symbol->function s)
> (case s
> [(adder) adder]
> [else (error "No function for ~a" s)]))
>
> Believe it or not, this is probably how you really want to do it.
Or may be with an extendable table (hash or associationlist) e,g:
(define env '())
(define (bind symbol value) (set! env (cons (cons symbol value) env)))
(define (lookup symbol) (cdr (or (assq symbol env) (error "lookup: undefined var:" symbol))))
(define (adder x y) (+ x y))
(bind 'adder adder)
(bind 'multiplier *)
((lookup 'adder) 3 4)
((lookup (string->symbol (string-append "multi" "plier"))) 3 4)
Jos
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>