unison/unison-src/demo/2.u

47 lines
1.1 KiB
Plaintext
Raw Normal View History

2018-11-12 22:36:47 +03:00
use Optional None Some
2019-03-30 01:02:36 +03:00
use Universal <
2018-11-12 22:36:47 +03:00
uncons : [a] -> Optional (a, [a])
uncons as = case at 0 as of
None -> None
Some hd -> Some (hd, drop 1 as)
halve : [a] -> ([a], [a])
halve s = splitAt (size s / 2) s
splitAt : Nat -> [a] -> ([a], [a])
splitAt n as = (take n as, drop n as)
merge : (a -> a -> Boolean) -> [a] -> [a] -> [a]
merge lte a b =
use Sequence ++
2018-11-12 22:36:47 +03:00
go out a b = case (uncons a, uncons b) of
(None,_) -> out ++ b
(_,None) -> out ++ a
(Some (hA, tA), Some (hB, tB)) ->
if hA `lte` hB then go (out `snoc` hA) tA b
else go (out `snoc` hB) a tB
go [] a b
sort : (a -> a -> Boolean) -> [a] -> [a]
2018-11-12 22:36:47 +03:00
sort lte as =
if size as < 2 then as
else case halve as of (left, right) ->
l = sort lte left
r = sort lte right
merge lte l r
-- let's make sure it works
2019-03-30 01:02:36 +03:00
> uncons [1, 2, 3]
> sort (<) [3,2,1,1,2,3,9182,1,2,34,1,23]
2019-03-30 01:02:36 +03:00
> sort (<) ["Dave", "Carol", "Eve", "Alice", "Bob", "Francis", "Hal", "Illy", "Joanna", "Greg", "Karen"]
2018-11-12 22:36:47 +03:00
-- these programs have some type errors
-- > sort (<) [3,2,1,1,2,3,9182,1,2,34,1,"oops"]
-- > merge (<) [1,4,5,90,102] ["a", "b"]