Haskell Design Patterns by Ryan Lemmer

Haskell Design Patterns by Ryan Lemmer

Author:Ryan Lemmer [Lemmer, Ryan]
Language: eng
Format: mobi, azw3, epub, pdf
Publisher: Packt Publishing
Published: 2015-11-05T23:00:00+00:00


F.foldrM :: (Foldable t, Monad m) =>

(a -> b -> m b) -> b -> t a -> m b

as in

doSum = F.foldrM doPlus

where

doPlus acc x = do

putStrLn $ (show x) ++ " = " ++ (show acc)

return (acc + x)

main = doSum 0 aTree

Foldable things can be expressed as lists by folding (:) over the Foldable, for example:

main = do

print $ F.toList aTree

-- same as

print $ F.foldr (:) [] aTree

This example shows that not all folding accumulates destructively. Fold accumulates the input structure into a single Monoid value, but that single value might be a composite structure. In general, fold transforms a data structure (often radically).

However, both map and filter are special cases of fold, which makes fold the fundamental function of structured recursion.

Foldable generalizes folding over arbitrary data structure and raises the concept of folding to type level. Foldable generalizes structured recursion in Haskell from lists to container types.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.