-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.hs
36 lines (28 loc) · 879 Bytes
/
sort.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
{-# language FlexibleInstances #-}
{-# language MultiParamTypeClasses #-}
{-# language FunctionalDependencies #-}
{-# LANGUAGE DeriveFunctor #-}
-- https://hackage.haskell.org/package/data-fix-0.2.0/docs/Data-Fix.html
import Data.Fix
split :: [a] -> ([a], [a])
split (a: b: t) = (a: t1, b: t2)
where
(t1, t2) = split t
split l = (l, [])
merge :: Ord a => [a] -> [a] -> [a]
merge (a: as) (b: bs) =
if a <= b
then a : merge as (b: bs)
else b : merge (a: as) bs
merge as [] = as
merge [] bs = bs
type Tree a = Fix (T a)
data T a b = Leaf | Node a b
deriving Functor
splitCoAlgrebra :: [a] -> T [a] [a]
splitCoAlgrebra [] = Leaf
splitCoAlgrebra s = Node s1 s2 where (s1,s2) = split(s)
mergeAlgebra :: Ord a => T [a] [a] -> [a]
mergeAlgebra Leaf = []
mergeAlgebra (Node s1 s2) = merge s1 s2
main = print (hylo mergeAlgebra splitCoAlgrebra [3,1,4,1,5,9])