表达式树评估
|
我正在尝试评估表达式树。这是我的代码:
(define (eval-tree expr-tree)
(eval-treeaux eval-tree expr-tree))
(define (eval-treeaux f expr-tree)
(if (null? expr-tree)
0
(cond ((equal? \'+ (operator expr-tree)) (+ (left-op expr-tree) (right-op expr-tree)))
((equal? \'- (operator expr-tree)) (- (left-op expr-tree) (right-op expr-tree)))
((equal? \'* (operator expr-tree)) (* (left-op expr-tree) (right-op expr-tree)))
((equal? \'/ (operator expr-tree)) (/ (left-op expr-tree) (right-op expr-tree)))
(else (f eval-treeaux expr-tree)))))
当我运行这个“ 1”时,它给了我这个错误:
+: expects type <number> as 1st argument, given: (6 * 3); other arguments
were: (4 - 2)
谁能告诉我问题出在哪里以及如何解决?
这是我的新代码:
(define (eval-tree expr-tree)
(if (null? expr-tree)
0
((eval-treeaux eval-tree (car expr-tree)) (eval-tree (cdr expr-tree)))))
(define (eval-treeaux f expr-tree)
(cond ((null? expr-tree) \'())
((not (isExpression expr-tree)) (list expr-tree))
(else (cond ((equal? \'+ (operator expr-tree)) (+ (left-op expr-tree) (right-op expr-tree)))
((equal? \'- (operator expr-tree)) (- (left-op expr-tree) (right-op expr-tree)))
((equal? \'* (operator expr-tree)) (* (left-op expr-tree) (right-op expr-tree)))
((equal? \'/ (operator expr-tree)) (/ (left-op expr-tree) (right-op expr-tree)))
(else (f eval-treeaux expr-tree))))))
但是,当我运行此(eval-tree \'((6 * 3) + (4 - 2)))
时,出现以下错误:
procedure application: expected procedure, given: 2; arguments were: 0
我真的很困惑。谁能帮我吗?
谢谢
没有找到相关结果
已邀请:
1 个回复
宠封钞轰
您需要递归计算子表达式,然后才能评估父表达式。