Question

I want to evaluate an expression in Racket with eval. For example my expression is : (+ x 1)... but I want x to be each value from a list of values. I have tried with let but I now know the eval doesn't see the variables defined with let. I have read abut namespaces but still, I don't really know how to apply them to my problem. Thank you !

Was it helpful?

Solution

Is this what you want?

> (map (lambda (n) (+ n 1)) '(2 5 3 1 99 12))
'(3 6 4 2 100 13)

If your expression is quoted, you could

(define ns (make-base-namespace))
(define expr '(+ x 1))
(for/list ((x '(2 5 3 1 99 12)))
  (eval `(let ((x ,x)) ,expr) ns))
=> '(3 6 4 2 100 13)

but I'm sure there's a better way if you provide more context.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top