0

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?

Community
  • 1
  • 1
monica
  • 1,035
  • 1
  • 9
  • 21

2 Answers2

2

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)
soegaard
  • 30,661
  • 4
  • 57
  • 106
1

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.

Community
  • 1
  • 1