[racket] eval question

From: prad (prad at towardsfreedom.com)
Date: Sat May 21 13:39:06 EDT 2011

Eli Barzilay <eli at barzilay.org> writes:

> About a minute ago, prad wrote:
>> if i run the last line in the repl, it works.
>> however, if i just try to run the program i the following error:
>> 
>> compile: unbound identifier (and no #%app syntax transformer is bound)
>> at: display in: (display phr)
>> 
>> so it seems that it doesn't recognize the function "display".
>> 
>> is it not able to access the original namespace then?
>> what is the solution for doing something like this?
>
> http://docs.racket-lang.org/guide/eval.html
>
thx eli - i finally figured it out ... and no it didn't take me since mar31
when you posted till may21 to find the solution. :D

i'm providing my attempts here in case it is helpful to someone else.

the idea was to see if i could execute a string within a datafile as a
racket command. furthermore, the function being executed is defined
within a module.

below are a program and a module illustrating possible ways of doing
some of this. they work, so presumably the code is correct though the
technique may be improved.


program: evaltst.rkt
====
#!/usr/bin/env racket
#lang racket

(require mzlib/string) ;if you want to use read-from-string function

;use base-namespace for existing functions
(define bns (make-base-namespace))
(eval '(+ 1 2) bns)


;use absolute/relative namespaces for functions defined locally
(define abs-ns (module->namespace
            (string->path "/home/pradmin/prog/racket/evaltst.rkt")))
(define rel-ns (module->namespace "evaltst.rkt"))

(define (show-it arg)
  (apply format "this is the argument you get: ~a" (list arg)))
(define thearg "ARGUMENT")

(eval (map string->symbol '("show-it" "thearg")) abs-ns)
(eval (read-from-string "(show-it thearg)") rel-ns)
(eval (read (open-input-string "(show-it thearg)")) rel-ns)


;relative namespace example for function defined in module
(require "evaltst-module.rkt")
(define mod-rel-ns (module->namespace "evaltst-module.rkt"))

(eval (read (open-input-string "(fn-in-module \"NICE ARGUMENT!\")"))
      mod-rel-ns)
====

module: evaltst-module.rkt
====
(module evaltst-module racket

  (define (fn-in-module arg)
    (apply format "result from fn-in-module: ~a" (list arg)))

)
====

i thought i needed to have a 
(provide fn-in-module)
in the latter, but it seems to work fine without this.


output
====
3
"this is the argument you get: ARGUMENT"
"this is the argument you get: ARGUMENT"
"this is the argument you get: ARGUMENT"
"result from fn-in-module: NICE ARGUMENT!"
====

i found the ability to access functions 'on the go' by embedding strings
into data to be really useful. what i do is have a line like this:
<rkt>(somefunction)</rkt> 
right in the datafile and then regexp the
'(somefunction)', execute it and then replace the line with the
result. this method allows the data to contain its destiny!


-- 
in friendship,
prad



Posted on the users mailing list.