## Concept:

# Foldable type

## Headline

A type for which a fold function can be defined

## Illustration

Obviously, a fold function can be defined for lists. See also the concept of Maybe type for another simple example of a foldable type. See the concept of rose tree for a more powerful illustration of a foldables.

In Language:Haskell, there is a type class of foldable types:

```
class Foldable t
where
fold :: Monoid m => t m -> m
foldMap :: Monoid m => (a -> m) -> t a -> m
foldr :: (a -> b -> b) -> b -> t a -> b
foldl :: (a -> b -> a) -> a -> t b -> a
foldr1 :: (a -> a -> a) -> t a -> a
foldl1 :: (a -> a -> a) -> t a -> a
```

The members *foldr* and *foldl* generalize the function signatures of the folklore fold functions for lists. It should be noted that a minimal complete definition requires either the definition of *foldr* or *foldMap*, as all other class members are then defined by appropriate defaults. Here is a particular attempt at such defaults:

```
class Foldable t
where
fold :: Monoid m => t m -> m
foldMap :: Monoid m => (a -> m) -> t a -> m
foldr :: (a -> b -> b) -> b -> t a -> b
foldl :: (a -> b -> a) -> a -> t b -> a
foldr1 :: (a -> a -> a) -> t a -> a
foldl1 :: (a -> a -> a) -> t a -> a
fold = foldr mappend mempty
foldMap f = foldr (mappend . f) mempty
foldr f z = foldr f z . toList
foldl f z q = foldr (\x g a -> g (f a x)) id q z
foldr1 f = foldr1 f . toList
foldl1 f = foldl1 f . toList
```

In a number of places, we leverage a conversion function *toList* for going from a foldable type over an element type to the list type over the same element type. In this manner, we can reduce some operations on foldables to operations on lists. This conversion function is easily defined by a *foldMap* application:

```
toList :: Foldable t => t a -> [a]
toList = foldMap (\x->[x])
```

Looking at the defaults again and their use of *toList*, there is obviously an "unsound" circularity within the definitions, which however would be soundly broken, when either *foldr* or *foldMap* was defined for any given foldable type.

## User contributions

## User edits

## Syntax for editing wiki

For you are available next options:will make text bold.

will make text italic.

will make text underlined.

will make text striked.

will allow you to paste code headline into the page.

will allow you to link into the page.

will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

will allow you to paste image into the page.

is list with bullets.

is list with numbers.

will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

will allow your to insert code snippets from
**
@worker.
**

## Syntax for editing wiki

For you are available next options:will make text bold.

will make text italic.

will make text underlined.

will make text striked.

will allow you to paste code headline into the page.

will allow you to link into the page.

will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

will allow you to paste image into the page.

is list with bullets.

is list with numbers.

will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

will allow your to insert code snippets from
**
@worker.
**