问题
I posted some other easy code, to clarify what's happening
When I use eval
on the following code
#lang racket
(define (test )
`( (define num 1)
(define l (list))
(define num2 (add1 num))
(displayln num2)))
(eval (test) (make-base-namespace))
racket screams at me define-values: not in a definition context in: (define-values (num) 1)
My questions are:
- How to make
eval
work on definition? - If
eval
is not designed to work on definitions, then is there some workarounds that can make it work?
I appreciate any help!
I think this might be an alternative way to the thing I want to do in here: How can I unsplice a list of expression into code?
回答1:
Here is an example:
#lang racket
(define ns (make-base-namespace))
(define top-level-expressions
'(begin
(define x 3)
(+ x 1)))
(eval top-level-expressions ns)
回答2:
As racket tells you, eval
needs an expression as argument. You should pass '(define len (make-length 10))
to eval
. Note the quote-sign '
.
But I am not sure if you really need to do that, please read about the purpose and the flaws of eval first.
来源:https://stackoverflow.com/questions/13428091/how-to-make-eval-work-on-define