Skip to content

Instantly share code, notes, and snippets.

@tippenein
Created March 24, 2013 01:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tippenein/5229968 to your computer and use it in GitHub Desktop.
Save tippenein/5229968 to your computer and use it in GitHub Desktop.
;; A small portion of the MUPL assignment
#lang racket
(provide (all-defined-out)) ;; so we can put tests in a second file
;; definition of structures for MUPL programs - Do NOT change
(struct var (string) #:transparent) ;; a variable, e.g., (var "foo")
(struct int (num) #:transparent) ;; a constant number, e.g., (int 17)
(struct add (e1 e2) #:transparent) ;; add two expressions
(struct ifgreater (e1 e2 e3 e4) #:transparent) ;; if e1 > e2 then e3 else e4
(struct fun (nameopt formal body) #:transparent) ;; a recursive(?) 1-argument function
(struct call (funexp actual) #:transparent) ;; function call
(struct mlet (var e body) #:transparent) ;; a local binding (let var = e in body)
(struct apair (e1 e2) #:transparent) ;; make a new pair
(struct fst (e) #:transparent) ;; get first part of a pair
(struct snd (e) #:transparent) ;; get second part of a pair
(struct aunit () #:transparent) ;; unit value -- good for ending a list
(struct isaunit (e) #:transparent) ;; evaluate to 1 if e is unit else 0
;; a closure is not in "source" programs; it is what functions evaluate to
(struct closure (env fun) #:transparent)
;; Problem 1
(define (racketlist->mupllist ls)
(if (null? ls)
(aunit)
(apair (car ls) (racketlist->mupllist (cdr ls)))))
(define (mupllist->racketlist ls)
(if (aunit? ls)
null
(cons (apair-e1 ls) (mupllist->racketlist (apair-e2 ls)))))
;; lookup a variable in an environment
(define (envlookup env str)
(cond [(null? env) (error "unbound variable during evaluation" str)]
[(equal? (car (car env)) str) (cdr (car env))]
[#t (envlookup (cdr env) str)]))
(define (eval-under-env e env)
(cond [(var? e)
(envlookup env (var-string e))]
[(add? e)
(let ([v1 (eval-under-env (add-e1 e) env)]
[v2 (eval-under-env (add-e2 e) env)])
(if (and (int? v1)
(int? v2))
(int (+ (int-num v1)
(int-num v2)))
(error "MUPL addition applied to non-number")))]
[(int? e) e]
[(fun? e) (closure env e)]
[(ifgreater? e)
(let ([v1 (eval-under-env (ifgreater-e1 e) env)]
[v2 (eval-under-env (ifgreater-e2 e) env)])
(if (and (int? v1)
(int? v2))
(if (> (int-num v1) (int-num v2))
(eval-under-env (ifgreater-e3 e) env)
(eval-under-env (ifgreater-e4 e) env))
(error "MUPL ifgreater applied to non-number")))]
[(mlet? e)
(let ([v1 (eval-under-env (mlet-e e) env)])
(eval-under-env (mlet-body e) (cons (cons (mlet-var e) v1) env)))]
[(call? e)
(let ([v1 (eval-under-env (call-funexp e) env)]
[v2 (eval-under-env (call-actual e) env)])
(if (closure? v1)
(let* ([c1 (closure-fun v1)]
[c2 (closure-env v1)]
[cn (cons (fun-nameopt c1) v1)]
[cf (cons (fun-formal c1) v2)])
(eval-under-env (fun-body c1) (if (eq? (car cn) #f)
(cons cf c2)
(cons cf (cons cn c2)))))
(error "MUPL call applied to non-closure")))]
[(apair? e) (apair (eval-under-env (apair-e1 e) env)
(eval-under-env (apair-e2 e) env))]
[(fst? e)
(let ([v1 (eval-under-env (fst-e e) env)])
(if (apair? v1)
(apair-e1 v1)
(error "MUPL fst applied to non-pair")))]
[(snd? e)
(let ([v1 (eval-under-env (snd-e e) env)])
(if (apair? v1)
(apair-e2 v1)
(error "MUPL snd applied to non-pair")))]
[(aunit? e) e]
[(isaunit? e) (if (aunit? (eval-under-env (isaunit-e e) env))
(int 1)
(int 0))]
[(closure? e) e]
[#t (begin (print e) (error "bad MUPL expression"))]))
(define (eval-exp e)
(eval-under-env e null))
@maxfriedrich42
Copy link

Dear tippenein, could you please delete this gist? You are giving away the solutions to graded assignments in the Coursera "Programming Languages" Course (which is currently offered again in self-paced mode). This makes it very easy to cheat and reduces the learning effect for other learners. Many thanks in advance for your collaboration in making the course more effective for everyone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment