Guest User

Untitled

a guest
Mar 13th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.04 KB | None | 0 0
  1.  
  2. (define-struct ae (fn args))
  3. (define testcase1 (make-ae '+ (list 'x (make-ae '* (list 'y 2)))))
  4. (define ass-lst1 '((x 2) (y 3) (z 4)))
  5.  
  6. (define (find-val key ass-lst)
  7.   (cond [(empty? ass-lst) #f]
  8.         [(symbol=? key (first (first ass-lst)))
  9.          (second (first ass-lst))]
  10.         [else (find-val key (rest ass-lst))]))
  11.  
  12. (define (eval1 expr ass-lst)
  13.   (cond [(number? expr) expr]
  14.         [else (apply1 (ae-fn expr) (ae-args expr) ass-lst)]))
  15.  
  16. (define (apply1 fn expr-lst ass-lst)
  17.   (cond [(empty? expr-lst)
  18.          (cond [(symbol=? fn '*) 1]
  19.                [(symbol=? fn '+) 0])]
  20.         [else
  21.             ((cond [(symbol=? fn '*) *]
  22.                    [(symbol=? fn '+) +])
  23.              (eval1
  24.               (cond [(or (number? (first expr-lst))
  25.                        (ae? (first expr-lst)))
  26.                      (first expr-lst)]
  27.                     [(symbol? (first expr-lst))
  28.                      (find-val (first expr-lst) ass-lst)]) ass-lst)
  29.              (apply1 fn (rest expr-lst) ass-lst))]))
  30.  
  31. (eval1 testcase1 ass-lst1)
Advertisement
Add Comment
Please, Sign In to add comment