Ich versuche, eine Funktion (eval t) zu implementieren, führt die folgenden:Schläger: implementieren Funktion (eval t)
Beispiele:
(eval '(2 * (1 + 2))) -> 6
(eval '((3 - (4/2)) * 2) -> 2
Was ich habe, so weit ist:
(define (inner lst)
((cond
((equal? (second lst) '+) +)
((equal? (second lst) '-) -)
((equal? (second lst) '*) *)
((equal? (second lst) '/) /))
(first lst) (third lst)))
(define (eval t)
(cond
((and (number? (first t)) (number? (third t))) (inner t))
((list? (third t)) (eval `(,(first t) ,(second t) ,(inner (third t)))))
((list? (first t)) (eval `(,(inner (first t)) ,(second t) ,(third t))))))
Es funktioniert für:
(eval '(1 + (1 + 2))) -> 4
(eval '((1 + 1) + (2 + 2))) -> 6
aber es funktioniert nicht für Fälle wie:
(eval '((1 + 1) + (1 + (1 + 1))))
Jede Hilfe wird sehr geschätzt werden!