Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (define-struct ae (fn args))
- (define testcase1 (make-ae '+ (list 'x (make-ae '* (list 'y 2)))))
- (define ass-lst1 '((x 2) (y 3) (z 4)))
- (define (find-val key ass-lst)
- (cond [(empty? ass-lst) #f]
- [(symbol=? key (first (first ass-lst)))
- (second (first ass-lst))]
- [else (find-val key (rest ass-lst))]))
- (define (eval1 expr ass-lst)
- (cond [(number? expr) expr]
- [else (apply1 (ae-fn expr) (ae-args expr) ass-lst)]))
- (define (apply1 fn expr-lst ass-lst)
- (cond [(empty? expr-lst)
- (cond [(symbol=? fn '*) 1]
- [(symbol=? fn '+) 0])]
- [else
- ((cond [(symbol=? fn '*) *]
- [(symbol=? fn '+) +])
- (eval1
- (cond [(or (number? (first expr-lst))
- (ae? (first expr-lst)))
- (first expr-lst)]
- [(symbol? (first expr-lst))
- (find-val (first expr-lst) ass-lst)]) ass-lst)
- (apply1 fn (rest expr-lst) ass-lst))]))
- (eval1 testcase1 ass-lst1)
Advertisement
Add Comment
Please, Sign In to add comment