[racket] Functions as symbols

From: Richard Cleis (rcleis at mac.com)
Date: Fri Oct 7 04:49:20 EDT 2011

The long answer to your question depends on how you are using the capability. You don't have to form the whole expression before evaluating it, and you don't need to use apply. For example, you can define a symbol in racket, then run the program:

#lang racket/base

(define sym '+)
[run the above]
 
Now you can recover the native function in the interactions pane like this:

> (define f (eval sym))
>

Then, f is what you want:

> (f 1 2)
> 3

The trouble is, the above is a subtle concoction of the top layer and the implied module created by #lang racket/base

You can read Guide:Racket, section 15.1, to figure out what you really need to do.

rac



On Oct 7, 2011, at 1:53 AM, Mark Carter wrote:

> I would like to treat a symbol as a function, and apply it to some arguments. In other words, if I did something like
> (define sym '+)
> (apply '+ '(1 2))
> I would obtain the answer 3. Is there any way I can do this? The following works, but I'm wondering if it's the best way:
> (eval `(apply ,sym '(1 2)))
> A more basic question would be: is there any way I can convert from '+ to + - i.e. a symbol to a function?
> 
> 
> _________________________________________________
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/users



Posted on the users mailing list.