Added Typed Racket.
WTF this language actually sucks.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
#lang pl
|
||||
|
||||
; I'm keeping my own type declarations 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.
|
||||
(define (gcd2 a b)
|
||||
(cond
|
||||
[(or (negative? a) (negative? b)) (error 'neg "Given negative number.")]
|
||||
[(= a 0) b]
|
||||
[(= b 0) a]
|
||||
[(and (even? a) (even? b)) (* 2 (gcd2 (floor (/ a 2)) (floor (/ b 2))))]
|
||||
[(even? b) (gcd2 a (floor (/ b 2)))]
|
||||
[(even? a) (gcd2 (floor (/ a 2)) b)]
|
||||
[else
|
||||
(if (<= a b)
|
||||
(gcd2 a (- b a))
|
||||
(gcd2 b (- a b)))]))
|
||||
|
||||
(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?
|
||||
(test (gcd2 2 -1) =error> "Given negative number.")
|
||||
|
||||
; out-of-bounds?: start: Number,
|
||||
; lower: Number,
|
||||
; upper: Number,
|
||||
; l: [Number] → Bool
|
||||
(: out-of-bounds? : (Number Number Number (Listof Number) -> Boolean))
|
||||
; Determines if an object that begins at the starting location and then moves delta by delta ever ever steps outside of the bounds (inclusive).
|
||||
(define (out-of-bounds? start lower upper l)
|
||||
(if (or (< start lower) (> start upper))
|
||||
#t
|
||||
(match l
|
||||
['() #f]
|
||||
[(cons frst rst) (out-of-bounds? frst lower upper rst)])))
|
||||
|
||||
(test (out-of-bounds? 0 -1 2 '(0 -2 3 -4 6)) => #t)
|
||||
(test (out-of-bounds? 0 -1 1 '(1)) => #f)
|
||||
(test (out-of-bounds? 0 -1 1 '()) => #f)
|
||||
(test (out-of-bounds? 0 1 2 '()) => #t)
|
||||
(test (out-of-bounds? 0 0 0 '()) => #f)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user