;;;;; ;;;;; ;;;;; Order ;;;;; ;;;;; (define \$ordering (algebraic-data-matcher { })) (define \$compare (lambda [\$m \$n] (if (collection? m) (compare-c m n) (if (lt? m n) (if (eq? m n) ))))) (define \$compare-c (lambda [\$c1 \$c2] (match [c1 c2] [(list something) (list something)] {[[ ] ] [[ _] ] [[_ ] ] [[ ] (compare-c xs ys)] [[ ] (compare x y)]}))) (define \$b.min (lambda [\$x \$y] (if (lt? x y) x y))) (define \$b.max (lambda [\$x \$y] (if (gt? x y) x y))) (define \$min/fn (lambda [\$compare \$x \$y] (if (eq? (compare x y) ) x y))) (define \$max/fn (lambda [\$compare \$x \$y] (if (eq? (compare x y) ) x y))) (define \$min (cambda \$xs (foldl b.min (car xs) (cdr xs)))) (define \$max (cambda \$xs (foldl b.max (car xs) (cdr xs)))) (define \$split-by-ordering (split-by-ordering/fn compare \$ \$)) (define \$split-by-ordering/fn (lambda [\$f \$p \$xs] (match xs (list something) {[ [{} {} {}]] [ (let {[[\$ys1 \$ys2 \$ys3] (split-by-ordering/fn f p rs)]} (match (f x p) ordering {[ [{x @ys1} ys2 ys3]] [ [ys1 {x @ys2} ys3]] [ [ys1 ys2 {x @ys3}]]}))]}))) (define \$sort (sort/fn compare \$)) (define \$sort/fn (lambda [\$f \$xs] (match xs (list something) {[ {}] [> {x}] [_ (let* {[\$n (length xs)] [\$p (nth (quotient n 2) xs)] [[\$ys1 \$ys2 \$ys3] (split-by-ordering/fn f p xs)]} {@(sort/fn f ys1) @ys2 @(sort/fn f ys3)})]}))) (define \$sort-strings (lambda [\$xs] (sort/fn 2#(compare-c (map ctoi (unpack %1)) (map ctoi (unpack %2))) xs))) (define \$merge (lambda [\$xs \$ys] (match [xs ys] [(list something) (list something)] {[[ _] ys] [[_ ] xs] [[ ] {x @(merge txs ys)}] [[_ ] {y @(merge xs tys)}]}))) (define \$merge/fn (lambda [\$f \$xs \$ys] (match [xs ys] [(list something) (list something)] {[[ _] ys] [[_ ] xs] [[ ) _>] {x @(merge txs ys)}] [[_ ] {y @(merge xs tys)}]})))