Created
October 5, 2018 22:04
-
-
Save jbclements/d99c78f371ef8d1c7ea98c897e3cedb7 to your computer and use it in GitHub Desktop.
afternoon code from friday
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 | |
(require typed/rackunit) | |
(define-type Value String) | |
(define-type ExprC (U ConcatC StrC)) | |
(struct ConcatC ([l : ExprC] [r : ExprC]) | |
#:transparent) | |
(struct StrC ([s : String]) #:transparent) | |
(ConcatC (StrC "abc") (StrC "def")) | |
;; parse an expression | |
(define (parse [s : Sexp]) : ExprC | |
(match s | |
[(? string? str) (StrC str)] | |
[(list l '+ r) (ConcatC (parse l) (parse r))] | |
[other (error 'parse "ouch!")])) | |
(check-equal? (parse '"apple") (StrC "apple")) | |
(check-equal? (parse '{"abc" + {"d" + "ef"}}) | |
(ConcatC (StrC "abc") | |
(ConcatC (StrC "d") | |
(StrC "ef")))) | |
;; evaluate an expression | |
(define (interp [exp : ExprC]) : Value | |
(match exp | |
[(StrC s) s] | |
[(ConcatC l r) (string-append (interp l) (interp r))])) | |
(check-equal? (interp (StrC "apple")) "apple") | |
(check-equal? (interp (ConcatC (StrC "abc") | |
(ConcatC (StrC "d") | |
(StrC "ef")))) | |
"abcdef") | |
#;((check-equal? (interp (parse '"apple")) "apple") | |
(check-equal? (interp (parse '{"abc" + "def"})) | |
)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment