A set of curated examples meant to show Haskell's expressiveness, wherein we write a sum
function many times:
With pattern matching:
sum :: (Num a) => [a] -> a
sum [] = 0
sum (x:xs) = x + sum xs
With a fold:
sum :: (Num a) => [a] -> a
sum = foldr (+) 0
With a catamorphism:
sum :: (Num a) => [a] -> a
sum = cata algebra where
algebra Nil = 0
algebra (Cons x xs) = x + xs
With the state monad:
sum :: (Num a) => [a] -> a
sum xs = flip execState 0 $
mapM_ (\x -> do { s <- get ; put $ x + s }) xs
An (inadvisable) approach using function composition:
sum :: (Num a) => [a] -> a
sum = ($ 0) . foldr (.) id . fmap (+)
Another (inadvisable) approach, this time using endomorphisms:
sum :: (Num a) => [a] -> a
sum = ($ 0) . appEndo . fold . fmap (Endo . (+))
Using monoidal sums:
sum :: (Num a) => [a] -> a
sum = getSum . mconcat . fmap Sum
Using lens and Matt Parsons's brain:
sum :: (Num a) => [a] -> a
sum = ala Sum foldMap