newLISP Fan Club

Forum => Anything else we might add? => Topic started by: cormullion on May 24, 2008, 08:23:04 AM

Title: Creating functions from strings
Post by: cormullion on May 24, 2008, 08:23:04 AM
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)
Title:
Post by: rickyboy on May 24, 2008, 09:00:28 AM
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
Title:
Post by: cormullion on May 24, 2008, 10:14:56 AM
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!
Title:
Post by: rickyboy on May 24, 2008, 10:53:21 AM
Quote from: "cormullion"Glad you haven't gone over to Arc completely, Ricky!

:-)