I hate this so much.

This commit is contained in:
2025-10-10 01:32:19 -04:00
parent 15e68d2dcc
commit 72872c1fb1
+45 -2
View File
@@ -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)