Last active
January 24, 2024 18:13
-
-
Save SHoltzen/8ec4d0ec1619a623ff8a4779072eb660 to your computer and use it in GitHub Desktop.
CS4400-Spr24-Ite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang plait | |
(define-type Value | |
[vbool (b : Boolean)] | |
[vnum (v : Number)]) | |
(define-type Exp | |
[num (n : Number)] | |
[bool (b : Boolean)] | |
[plus (left : Exp) (right : Exp)] | |
[cnd (test : Exp) (thn : Exp) (els : Exp)]) | |
(define (get-num-or-err value) | |
(type-case Value value | |
[(vnum n) n] | |
[else (error 'value "value conversion error")])) | |
(define (get-bool-or-err value) | |
(type-case Value value | |
[(vbool b) b] | |
[else (error 'value "value conversion error")])) | |
(define (add v1 v2) | |
(+ (get-num-or-err v1) (get-num-or-err v2))) | |
(define (calc e) | |
(type-case Exp e | |
([num n] (vnum n)) | |
([bool b] (vbool b)) | |
([plus l r] (vnum (add (calc l) (calc r)))) | |
([cnd test thn els] (if (get-bool-or-err (calc test)) | |
(calc thn) | |
(calc els))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment