-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathRLE.hs
43 lines (33 loc) · 1.27 KB
/
RLE.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
37
38
39
40
41
42
43
{-# LANGUAGE FlexibleContexts #-}
module RLE where
import GHC.List
-- This used to all be polymorphic in the element type,
-- to show to to work with an unconstrainted `patternFailure` term.
--
-- Now `patternFailure` has [`{Default E}] and this does no longer work.
-- So I changed this example to be monomorphic...
type E = Int
hd :: [E] -> E
hd (x:xs) = x
tl :: [E] -> [E]
tl (x:xs) = xs
group :: Eq E => [E] -> [[E]]
group = groupBy (==)
-- | The 'groupBy' function is the non-overloaded version of 'group'.
{- Not accepted by coqs termination checker. Function would probably work.
groupBy :: (E -> E -> Bool) -> [E] -> [[E]]
groupBy _ [] = []
groupBy eq (x:xs) = (x:ys) : groupBy eq zs
where (ys,zs) = span (eq x) xs
-}
-- Structurally recursive version of groupBy
groupBy :: (E -> E -> Bool) -> [E] -> [[E]]
groupBy f [] = []
groupBy f (x:xs) = case groupBy f xs of
[] -> [[x]]
(y:ys) : yss | f x y -> (x:y:ys) : yss
| otherwise -> [x] :(y:ys) : yss
rle :: Eq E => [E] -> [(E,Int)]
rle ys = map (\x -> (hd x, GHC.List.length x)) (group ys)
bad_rle :: Eq E => [E] -> [(E,Int)]
bad_rle ys = map (\x -> (hd (tl x), GHC.List.length x)) (group ys)