trees-tests.mlish (1249B)
1 #lang s-exp "../../mlish.rkt" 2 (require "../rackunit-typechecking.rkt") 3 (require "trees.mlish") 4 5 (define (make [item : Int] [depth : Int] -> (Tree Int)) 6 (if (zero? depth) 7 (Leaf item) 8 (let ([item2 (* item 2)] 9 [depth2 (sub1 depth)]) 10 (Node (make (sub1 item2) depth2) 11 item 12 (make item2 depth2))))) 13 14 (define tree1 (make 4 1)) 15 (define tree2 (make 3 2)) 16 17 (check-type tree1 18 : (Tree Int) -> (Node (Leaf 7) 4 (Leaf 8))) 19 20 (check-type tree2 21 : (Tree Int) 22 -> (Node 23 (Node (Leaf 9) 5 (Leaf 10)) 24 3 25 (Node (Leaf 11) 6 (Leaf 12)))) 26 27 (define (sum [t : (Tree Int)] -> Int) 28 (match t with 29 [Leaf v -> v] 30 [Node l v r -> (+ (+ (sum l) v) (sum r))])) 31 32 (check-type (sum tree1) : Int -> 19) 33 (check-type (sum tree2) : Int -> 56) 34 35 (define (check/acc [t : (Tree Int)] [acc : Int] -> Int) 36 (match t with 37 [Leaf v -> 38 (+ acc v)] 39 [Node l v r -> 40 (check/acc l (- acc (check/acc r 0)))])) 41 (define (check [t : (Tree Int)] -> Int) 42 (check/acc t 0)) 43 44 (define min-depth 4) 45 46 (define (main [n : Int] -> Int) 47 (let* ([max-depth (max (+ min-depth 2) n)] 48 [stretch-depth (add1 max-depth)]) 49 (check (make 0 stretch-depth)))) 50 51 (check-type (main 17) : Int -> 0)