3 data SList a = Nil | Cons a (SList a)
5 deriving instance (Ord a) => Ord (SList a)
6 deriving instance (Show a) => Show (SList a)
9 mapSList :: (a -> <e> b) -> SList a -> <e> SList b
12 loop (Cons h t) = Cons (f h) (loop t)
16 mapFirstSList :: (a -> <e> Maybe b) -> SList a -> <e> Maybe b
17 mapFirstSList f l = loop l
19 loop (Cons h t) = match f h with
25 iterSList :: (a -> <e> dummy) -> SList a -> <e> ()
26 iterSList f l = loop l
34 anySList :: (a -> <e> Boolean) -> SList a -> <e> Boolean
37 loop (Cons h t) = f h || loop t
41 allSList :: (a -> <e> Boolean) -> SList a -> <e> Boolean
44 loop (Cons h t) = f h && loop t
48 foldlSList :: (a -> b -> <e> a) -> a -> SList b -> <e> a
49 foldlSList f init l = loop init l
51 loop cur (Cons h t) = loop (f cur h) t
55 foldl1SList :: (a -> a -> <e> a) -> SList a -> <e> a
56 foldl1SList f (Cons h t) = foldlSList f h t
58 foldrSList :: (b -> a -> <e> a) -> a -> SList b -> <e> a
59 foldrSList f cur (Cons h t) = f h (foldrSList f cur t)
60 foldrSList f cur _ = cur
62 dropSList :: Integer -> SList a -> SList a
63 dropSList n l | n<=0 = l
64 dropSList n (Cons h t) = dropSList (n-1) t
65 dropSList n l = l /* = Nil */
67 takeSList :: Integer -> SList a -> SList a
68 takeSList i _ | i <= 0 = Nil
69 takeSList i (Cons h t) = Cons h (takeSList (i-1) t)
72 lengthSList :: SList a -> Integer
73 lengthSList l = loop 0 l
75 loop accum (Cons _ t) = loop (accum+1) t
78 isEmptySList :: SList a -> Boolean
79 isEmptySList Nil = True
80 isEmptySList _ = False
83 singletonSList :: a -> SList a
84 singletonSList e = Cons e Nil
86 reverseSList :: SList a -> SList a
87 reverseSList l = loop Nil l
89 loop accum (Cons h t) = loop (Cons h accum) t
92 sortSList :: Ord a => SList a -> SList a
93 sortSList l = sortAux (lengthSList l) l
95 sortAux n l | n <= 1 = l
96 sortAux n l = let half = n `div` 2
97 (left,right) = split half Nil l
98 in merge (sortAux half left) (sortAux (n-half) right)
99 split n accum l | n<=0 = (accum, l)
100 split n accum (Cons h t) = split (n-1) (Cons h accum) t
101 merge l1@(Cons h1 t1) l2@(Cons h2 t2)
102 | h1 <= h2 = Cons h1 (merge t1 l2)
103 | otherwise = Cons h2 (merge l1 t2)
107 appendSList :: SList a -> SList a -> SList a
108 appendSList (Cons h t) l = Cons h (appendSList t l)
111 nthSList :: SList a -> Integer -> a
112 nthSList Nil _ = fail "Index out of range"
113 nthSList (Cons h _) 0 = h
114 nthSList (Cons _ t) i = nthSList t (i-1)
117 toListSList :: SList a -> [a]
118 toListSList l = build (\empty cons ->
119 let loop accum (Cons h t) = loop (cons accum h) t
124 fromListSList :: [a] -> SList a
125 fromListSList = foldr Cons Nil