IRC channel logs

2014-11-09.log

back to list of logs

<jmd>How can I convert a string to a procedure ?
<alezost>jmd: (define my-proc (const "string"))
<jmd>hi
<jmd>Why doesn't this work: (let ((fred 3)) (eval-string "(display (* fred 4))"))
<taylanub>jmd: Scheme generally doesn't support that level of "reflection" because it would thwart optimization possibilities. that being said, there's some 'local-eval' in Guile somewhere IIRC.
<alezost>jmd: because that string has no idea about your fred variable
<jmd>alezost: So how do I give it an idea about it?
<alezost>jmd: (eval-string "(let ((fred 3)) (display (* fred 4)))")
<taylanub>ideally you should find a better solution to your problem, which won't require such a construct. if you really want it, look into that local-eval thing .. lemme see where it was
<jmd>I tried creating a new environment with fred defined and passing that so eval-string, but no luck.
<jmd>alezost: I cannot change the string. It is passed to me from another process.
<taylanub>(use-modules (ice-9 local-eval))
<taylanub>might open a security hole though, if you just eval strings given to you...
<ijp>((@@ (ice-9 secret-hax) launch-the-missiles))
<jmd>Unfortunately there doesn't seem to be a local-eval-string procedure.
<ijp>compose local-eval read and open-input-string
<taylanub>jmd: what exactly is your program doing that requires eval?
<jmd>./myprog "(* x 2)"
<jmd>where x id defined inside myprog.
<taylanub>jmd: ok, interesting. would construct an environment where that's defined and eval in that then. you can use `read' to get a sexpr from a string.
<taylanub>you have to loop with `read' if there's several sexprs allowed like "(* x 2) (/ x 3)"
<taylanub>(let loop ((sexprs '())) (let ((sexpr (read))) (if (eof-object? sexpr) (reverse sexprs) (loop (cons sexpr sexprs)))))
<jmd>Then how do I evaluate the sexpr ?
<taylanub>(eval sexpr env) well, having several sexprs, you'd (for-each (lambda (sexpr) (eval sexpr env)) sexprs). you can get an env by constructing a module...
<jmd>ok thanks. I will try something like that.
<taylanub>(info "(guile) Module System Reflection") here's some functions to work with modules imperatively
<taylanub>don't have any exact code in mind right now .oO( if only it were as clean in R7RS! )
<taylanub>*as in R7RS
*taylanub be back in half an hour
<mark_weaver>jmd: if you give me a broader view of what you're working on that needs to evaluate strings with access to local variables, I might be able to make some suggestions.
<mark_weaver>jmd: two ideas that come to mind are: (1) local-eval, or (2) make those variables top-level bindings in a module, and then pass that module to 'eval-string' (it's an optional keyword argument).
<jmd>mark_weaver: Thanks to taylanub I think I realise now that I don't need the local_eval. The (call-with-string proc read) was what I really needed.