
Untitled
By: a guest on
Jun 17th, 2012 | syntax:
None | size: 0.96 KB | hits: 12 | expires: Never
{-
λ> toRList [13,81,80,79,78,77,76,19,40,41,42,43,44,45,48]
Cons 13 (Sequence 81 76 (Cons 19 (Sequence 40 45 (Cons 48 Nil))))
λ> fromRList (Cons 13 (Sequence 81 76 (Cons 19 (Sequence 40 45 (Cons 48 Nil)))))
[13,81,80,79,78,77,76,19,40,41,42,43,44,45,48]
-}
data RList = Cons Int RList
| Sequence Int Int RList
| Nil
deriving (Show, Eq, Ord)
toRList :: [Int] -> RList
toRList = foldr f Nil
where
f :: Int -> RList -> RList
f x list = case list of
Nil -> Cons x Nil
Cons y l ->
if x `elem` [y-1, y+1] then Sequence x y l
else Cons x list
Sequence f t l ->
if (x == f+1 && f > t) || (x == f-1 && f < t) then Sequence x t l
else Cons x list
fromRList :: RList -> [Int]
fromRList list = case list of
Nil -> []
Cons x l -> x : fromRList l
Sequence f t l ->
(if f > t then reverse [t..f] else [f..t]) ++ fromRList l