55 lines
1.7 KiB
Plaintext
55 lines
1.7 KiB
Plaintext
;;;; (max-value T) and (min-value T): a numeric type's limits, named by the type, at
|
|
;;;; a concrete type and inside a generic whose bound admits numbers.
|
|
|
|
;; A selection sort, descending, whose running best starts at the least value
|
|
;; of the element type, so any element beats it.
|
|
(defn sort-desc [s [$t]] ()
|
|
{:where (is-numeric $t)}
|
|
(dotimes [i (length s)]
|
|
(let [best (min-value $t)
|
|
at-best i]
|
|
(dotimes [j (- (length s) i)]
|
|
(let [k (+ i j)]
|
|
(when (> (at s k) best)
|
|
(set best (at s k))
|
|
(set at-best k))))
|
|
(swap s i at-best))))
|
|
|
|
(defn largest [s [$t]] $t
|
|
{:where (is-numeric $t)}
|
|
(let [best (min-value t)]
|
|
(dotimes [i (length s)]
|
|
(when (> (at s i) best) (set best (at s i))))
|
|
best))
|
|
|
|
(defn show-i32 [s [i32]] ()
|
|
(dotimes [i (length s)] (print (at s i)) (print " "))
|
|
(println ""))
|
|
|
|
(defn show-f64 [s [f64]] ()
|
|
(dotimes [i (length s)] (print (at s i)) (print " "))
|
|
(println ""))
|
|
|
|
(defn main [] i32
|
|
(println (max-value u8))
|
|
(println (min-value u8))
|
|
(println (max-value i8))
|
|
(println (min-value i8))
|
|
(println (max-value i32))
|
|
(println (min-value i64))
|
|
(println (max-value u64))
|
|
(println (= (max-value f32) f32-max))
|
|
(println (= (min-value f64) (- f64-max)))
|
|
(println (= (max-value i16) i16-max))
|
|
(let [a [(i32 3) -7 12 0 -2147483648 5]
|
|
b [2.5 -1.0 1e300 -1e308]
|
|
c [(u8 4) 0 200 9]]
|
|
(sort-desc (slice a))
|
|
(show-i32 (slice a))
|
|
(sort-desc (slice b))
|
|
(show-f64 (slice b))
|
|
(println (largest (slice c)))
|
|
(println (let [d [(i64 -5) -9]] (largest (slice d))))
|
|
(println (let [e [(f32 -1.0) -3.0]] (= (largest (slice e)) (f32 -1.0)))))
|
|
0)
|