2
0
Fork 0
mirror of https://github.com/MartinThoma/LaTeX-examples.git synced 2025-04-25 22:38:04 +02:00

Haskell-Beispiele hinzugefügt

This commit is contained in:
Martin Thoma 2014-03-18 19:31:04 +01:00
parent 137a44cd5e
commit 8e73ed0910
6 changed files with 69 additions and 2 deletions

View file

@ -0,0 +1,21 @@
splitWhen :: (a -> Bool) -> [a] -> ([a], [a])
splitWhen _ [] = ([], [])
splitWhen p (x:xs)
| p x = ([], x:xs)
| otherwise = let (ys, zs) = splitWhen p xs
in (x:ys, zs)
-- >>> splitWhen even [1,2,3]
-- ([1],[2,3])
group :: Eq a => [a] -> [[a]]
group [] = []
group (x:xs) = let (group1, rest) = splitWhen (/=x) xs
in (x:group1) : group rest
encode :: Eq a => [a] -> [(a, Int)]
encode xs = map (\x -> (head x, length x)) (group xs)
decode [] = []
decode ((x,n):xs) = replicate n x ++ decode xs
-- alternativ
decode = concat . (map (\(x,n)->replicate n x))