未绑定标识符球拍运营商

Unbound identifier racket operators

这是我第一次使用球拍,尝试在 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) 工作正常,但当我执行此代码时,它会出现一个未绑定的标识符。任何帮助表示赞赏。

您调用 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 参数,请考虑将其删除。

这里不需要eval。而不是 return 符号 return 而是程序:

#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)