Creating functions from strings

For the Compleat Fan
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Creating functions from strings

Post by cormullion »

Is there a way to create a function definition if you have the body as a string?

Eg

Code: Select all

(set 'body "(println x) (exit)")
(define (func) body)

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Post by rickyboy »

Hey cormullion,

I don't know exactly what you're driving at, but does this help?

Code: Select all

(define (string2func str func-name)
  (eval-string (string "(define (" func-name ") " str ")")))

(set 'x 42)
(set 'body "(println \"Hello\") (println x)")

> (string2func body 'func)
(lambda () (println "Hello") (println x))
> (func)
Hello
42
42
(λx. x x) (λx. x x)

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Ah, yes - I was thinking about list and string. I must have forgotten that eval-string doesn't always run immediately...

Thanks! Glad you haven't gone over to Arc completely, Ricky!

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Post by rickyboy »

cormullion wrote:Glad you haven't gone over to Arc completely, Ricky!
:-)
(λx. x x) (λx. x x)

Locked