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:
parent
137a44cd5e
commit
8e73ed0910
6 changed files with 69 additions and 2 deletions
|
@ -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))
|
Loading…
Add table
Add a link
Reference in a new issue