lengthr, lengthl :: [a] -> Int
lengthr = foldr (\_ n -> n+1) 0
lengthl = foldl (\n _ -> n+1) 0
append :: [a] -> [a] -> [a]
append = flip (foldr (:))
mapr :: (a -> b) -> [a] -> [b]
mapr f = foldr (\x -> (f x:)) []
unzipr :: [(a,b)] -> ([a],[b])
unzipr = foldr (\ (x,y) (xs,ys) -> (x:xs,y:ys)) ([],[])
nubr :: Eq a => [a] -> [a]
nubr = foldr (\x xs -> x : filterr (x/=) xs) []
filterr :: (a -> Bool) -> [a] -> [a]
filterr p = foldr (\x xs -> if p x then x:xs else xs) []
reversel, reverser :: [a] -> [a]
reversel = foldl (flip (:)) []
reverser xs = foldr (\y g -> (\ys -> g (y:ys))) id xs []
-- foldl mit foldr:
foldl' f v xs = foldr (\x g -> (\a -> g (f a x))) id xs v
{- umgekehrt geht nicht, da foldl strikt im Listenargument, foldr aber nicht
==> foldr ist 'allgemeiner' als foldl
Wer mehr wissen moechte, lese:
Graham Hutton - A tutorial on the universality and expressiveness of fold
-}