Last active
August 29, 2015 14:07
-
-
Save wolverian/956bd5f1afb9fd6e5240 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 typed/racket | |
(define-syntax deftype | |
(syntax-rules () | |
[(_ name [cons-name (field-name : field-type) ...] ...) | |
(begin | |
(struct cons-name ([field-name : field-type] ...) #:transparent) ... | |
(define-type name (U cons-name ...)))])) | |
(deftype arith-c | |
[num-c (n : Number)] | |
[plus-c (l : arith-c) (r : arith-c)] | |
[mult-c (l : arith-c) (r : arith-c)]) | |
(: eval-arith (arith-c -> Number)) | |
(define (eval-arith expr) | |
(match expr | |
[(num-c n) n] | |
[(plus-c l r) (+ (eval-arith l) (eval-arith r))] | |
[(mult-c l r) (* (eval-arith l) (eval-arith r))])) | |
(: parse-arith (Sexp -> arith-c)) | |
(define (parse-arith expr) | |
(match expr | |
[(? number? n) (num-c n)] | |
[(list + l r) (plus-c (parse-arith l) (parse-arith r))] | |
[(list * l r) (mult-c (parse-arith l) (parse-arith r))])) | |
(eval-arith (parse-arith (+ (* 42 2) (* 2 (+ 1 0))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment