## 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.

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.

## Metadata

### There are no revisions for this page.

## 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.
**

#### 10 most similar pages:

- Concept:Rose tree - 0.9640439
- Concept:Fold_function - 0.9531589
- Concept:List monoid - 0.9127586
- Concept:Monoid - 0.6681804
- Concept:Fold algebra - 0.55879855
- Concept:Sum monoid - 0.49650386
- Concept:Map function - 0.4594645
- Concept:Functor - 0.4094705
- Concept:Higher-order function - 0.398339
- Concept:Type class - 0.39282608

## 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.
**