Skip to content

Instantly share code, notes, and snippets.

@wilbowma
Created February 3, 2015 02:32
Show Gist options
  • Select an option

  • Save wilbowma/daf536c53c34b4dbea46 to your computer and use it in GitHub Desktop.

Select an option

Save wilbowma/daf536c53c34b4dbea46 to your computer and use it in GitHub Desktop.
#lang racket
(define lookup (compose cdr assoc))
(define (extend-env x e env) (cons `(,x . ,e) env))
(define init-env
`((zero? . ,zero?)
(sub1 . ,sub1)
(add1 . ,add1)
(* . ,*)
(+ . ,+)
(- . ,-)
(/ . ,/)))
(define (eval foo)
(let eval ([foo foo]
[env init-env])
(match foo
[(? symbol? x) (lookup x env)]
[(? number? x) x]
[`(if ,e ,t ,f) (if (eval e env) (eval t env) (eval f env))]
[`(lambda (,x) ,e) (lambda (y) (eval e (extend-env x y env)))]
[`(,e1 ,e2) ((eval e1 env) (eval e2 env))])))
(module+ test
(require rackunit)
(check-equal? 0 (eval `(sub1 1)))
(check-equal? 2 (eval `(add1 1))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment