From 72872c1fb126f7142ac3577aa114074e2ee5a96a Mon Sep 17 00:00:00 2001 From: Jacob Signorovitch Date: Fri, 10 Oct 2025 01:32:19 -0400 Subject: [PATCH] I hate this so much. --- 04-typed-racket/main.rkt | 47 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/04-typed-racket/main.rkt b/04-typed-racket/main.rkt index b2cfb91..5e60ffc 100644 --- a/04-typed-racket/main.rkt +++ b/04-typed-racket/main.rkt @@ -1,6 +1,6 @@ #lang pl -; I'm keeping my own type declarations bc the racket ones are literally unreadable. +; I'm keeping my own type declarations in bc the racket ones are literally unreadable. ; gcd2: a: Int, b: Int → Int (: gcd2 : (Integer Integer -> Integer)) ; Given two non-negative integers, determine their greatest common divisor. @@ -19,7 +19,7 @@ (test (gcd2 0 0) => 0) (test (gcd2 288 64) => 32) -; What's the point of the symbol in the error definition if it isn't used to check errors? +; What's the point of the symbol in the error definition if it isn't even used to compare errors? (test (gcd2 2 -1) =error> "Given negative number.") ; out-of-bounds?: start: Number, @@ -41,4 +41,47 @@ (test (out-of-bounds? 0 1 2 '()) => #t) (test (out-of-bounds? 0 0 0 '()) => #f) +; A binary tree. +(define-type BT + [Node Number BT BT] + [End '()]) ; Couldn't figure out a better way to do this. +(define end (End '())) +(define sprout (Node 0 end end)) +(define seedling (Node 5 sprout (Node 6 end end))) +(define sapling (Node 10 + (Node 6 sprout end) + (Node 12 end end))) +(define deeply-broke-tree + (Node 10 + (Node 6 end (Node 11 end end)) + (Node 12 end end))) + +(: good-bt? : (BT -> Boolean)) +; good-bt?: t BT → Bool +; Determine whether bt is good. +(define (good-bt? t) + () + #| + (local [; <2: U{Num, Sym}, Num, U{Num, Sym} → Bool + ; Convenient form of greater than. Slightly hack. + (define (<2 a b c) + (cond [(and (symbol? a) (symbol? c)) #t] + [(symbol? a) (< (- b 1) b c)] + [(symbol? c) (< a b (+ b 1))] + [else (< a b c)])) + + ; recurse: t: Tree, lower: Num, upper: Num → Bool + ; Recursively checks left & right with lower & upper bounds. + (define (recurse t lower upper) + (cond [(symbol? t) (symbol=? t 'end)] + [else (and (<2 lower (tree-n t) upper) + (recurse (tree-left t) lower (tree-n t)) + (recurse (tree-right t) (tree-n t) upper))]))] + (recurse t 'quite-little 'very-big)))|# + +(check-expect (bst? sprout) #t) +(check-expect (bst? sapling) #t) +(check-expect (bst? good-tree) #t) +(check-expect (bst? broke-tree) #f) +(check-expect (bst? deeply-broke-tree) #f) \ No newline at end of file