Created
January 9, 2019 18:56
-
-
Save jbclements/171ab8f72987ad8d0999482b88d763e4 to your computer and use it in GitHub Desktop.
code from class, 2019-01-09
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) | |
;; given a number, return the fizzbuzz game string | |
(define (fizzbuzz-num [n : Natural]) : String | |
(cond [(= 0 (modulo n 15)) "fizzbuzz"] | |
[(= 0 (modulo n 3)) "fizz"] | |
[(= 0 (modulo n 5)) "buzz"] | |
[else (number->string n)])) | |
(check-equal? (fizzbuzz-num 9) "fizz") | |
(check-equal? (fizzbuzz-num 10) "buzz") | |
(check-equal? (fizzbuzz-num 11) "11") | |
(check-equal? (fizzbuzz-num 15) "fizzbuzz") | |
(define-type Text (U Poem Essay)) | |
(struct Poem ([title : String] [lines : Natural] [rhymes? : Boolean]) | |
#:transparent) | |
(struct Essay ([title : String] [thesis : String]) | |
#:transparent) | |
(Poem "My Left Arm" 6 #f) | |
(Essay "My Right Arm" "My right hand is better than my left.") | |
;; does the string start with q? | |
(define (starts-with-q? [s : String]) | |
(equal? (string-ref s 0) #\Q)) | |
;; return the first character of the title | |
(define (text-first-char [t : Text]) : Char | |
(match t | |
;; silly extra match clause to illustrate how the `?` pattern works: | |
[(Poem (? starts-with-q? _) _ _) #\Q] | |
[(Poem t _ _) (string-ref t 0)] | |
[(Essay t _) (string-ref t 0)])) | |
(check-equal? (text-first-char (Poem "The Lion Roars" | |
12 #t)) | |
#\T) | |
(check-equal? (text-first-char (Essay "On Liberty" | |
"Politics is complicated.")) | |
#\O) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment