Skip to content

Instantly share code, notes, and snippets.

@SHoltzen
Created January 24, 2024 17:28
Show Gist options
  • Save SHoltzen/38fa940cf6d3a474dcb3dd6a3d1820d5 to your computer and use it in GitHub Desktop.
Save SHoltzen/38fa940cf6d3a474dcb3dd6a3d1820d5 to your computer and use it in GitHub Desktop.
CS4400-Spr24-Calc
#lang plait
(define-type Exp
[num (n : Number)]
[plus (left : Exp) (right : Exp)])
(parse : (S-Exp -> Exp))
(define (parse s)
(cond
[(s-exp-number? s)
(num (s-exp->number s))]
[(s-exp-list? s)
(let ([l (s-exp->list s)])
(cond
[(empty? l) (error 'parse "empty list")]
[(symbol=? '+
(s-exp->symbol (first l)))
(plus (parse (second l))
(parse (third l)))]
[else (error 'parse "unrecognized symbol")]))]))
;; evaluates a Calc expression to a number
(calc : (Exp -> Number))
(define (calc e)
(type-case Exp e
[(num n) n]
[(plus l r) (+ (calc l) (calc r))]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment