首页 文章

未绑定的标识符球拍操作符

提问于
浏览
2

这是我第一次使用racket,在尝试评估Dr. Racket中的列表时收到错误消息(*:未绑定标识符;) .

#lang racket
(define (randop)
  (define x(random 3))
  (cond
    ((= x 0) '+)
    ((= x 1) '-)
    ((= x 2) '*)
   )
)

(define (randexp ht)
   (define x(random 10))
   (define y(random 10))
   (define z(randop))
  (eval (list z y x))
)

(randexp 1)

在控制台中执行racket时,(eval lst)工作正常,但是当我执行此代码时,它会提供一个未绑定的标识符 . 任何帮助表示赞赏 .

2 回答

  • 3

    有's a problem with the way you'重新调用 eval ,在Racket中你必须在一个文件中执行此操作:

    (define-namespace-anchor a)
    (define ns (namespace-anchor->namespace a))
    
    (define (randop)
      (define x (random 3))
      (cond
        ((= x 0) '+)
        ((= x 1) '-)
        ((= x 2) '*)))
    
    (define (randexp ht)
      (define x (random 10))
      (define y (random 10))
      (define z (randop))
      (eval (list z y x) ns))
    
    (randexp 1)
    

    此外,您实际上并没有使用 ht 参数,请考虑删除它 .

  • 2

    你在这里不需要评估 . 而不是返回符号而不是返回程序:

    #lang racket
    
    (define (randop)
      (define x (random 3))
      (cond ((= x 0) +) ; + not quoted means if will return what + evaluates to
            ((= x 1) -) ; which is the procedures they represent
            ((= x 2) *)))
    
    (define (randexp)
       (define x (random 10))
       (define y (random 10))
       (define z (randop))
       (z y x))) ; call z (the procedure returned by randop) with arguments x and y.
    
    (randexp)
    

相关问题