Creating functions from strings

Started by cormullion, May 24, 2008, 08:23:04 AM

Previous topic - Next topic

cormullion

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



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

rickyboy

#1
Hey cormullion,



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


(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

#2
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

#3
Quote from: "cormullion"Glad you haven't gone over to Arc completely, Ricky!

:-)
(λx. x x) (λx. x x)