我有以下代码:
(define (eval-1 exp)
; display expressions added. -- dlb
(display "Executing eval-1, exp = ")
(displayln exp)
(cond ((constant? exp)
(displayln "constant? is true")
exp)
((symbol? exp)
(displayln "symbol? is true")
(eval exp)) ; use underlying Racket's EVAL
((quote-exp? exp) ; *CHANGE* Modified to check if the length of the input is 2 as 2 inputs is only 1 value and an expression.
(if (= (length exp) 2)
(begin
(displayln "quote? is true")
(cadr exp))
(error "Quote expression must have exactly one argument"))) ; *CHANGE* Throw an error and terminate if more than 1 value is input.
((if-exp? exp)
(displayln "if-exp? is true")
(if (eval-1 (cadr exp)) ; use underlying Racket's IF
(eval-1 (caddr exp))
(eval-1 (cadddr exp))))
((lambda-exp? exp)
(displayln "lambda-exp? is true")
exp)
((map-exp? exp)
(displayln "map-exp? is true")
(apply-1 (eval-1 (cadr exp)) (map eval-1 (cddr exp))));add in (map-1 (lambda (x) (* x x)) '(1 2 3 4))
((pair? exp)
(displayln "pair? is true")
(apply-1 (eval-1 (car exp)) ; eval the operator
(map eval-1 (cdr exp))))
(else (error "bad expr: " exp))))
它在一个临时方案解释器中,定义为 (define (eval-1 exp) 处理诸如 lambda、if 等的事情。我还有一个帮助器可以识别何时输入 map-1 命令。我的 map-1当我输入“(map-1 + '(1 2 3 4) '(10 20 30 40))”以及每当我尝试输入像“(map-1 (lambda (x) (*) x x)) '(1 2 3 4))" 我总是遇到违反合同的情况,我不确定如何做到这一点,以便这两个测试用例都能通过。
我尝试单独使用 map 以及添加 apply map,发现 apply map 可以处理值,但不能处理 lambdas。