Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The missing folds

If you want to fold a list, I see four ways to do it.

Fold from the right of the list, with the recursive term on the right

foldrr (-) 100 [1..10] = 1 - (2 - (3 - (4 - (5 - (6 - (7 - (8 - (9 - (10 - (100)))))))))) = 95

foldrr :: (a -> b -> b) -> b -> [a] -> b
foldrr step zero (x:xs) = step x (foldrr step zero xs)
foldrr _    zero []     = zero

Fold from the right of the list, with the recursive term on the left

foldrl (-) 100 [1..10] = ((((((((((100) - 10) - 9) - 8) - 7) - 6) - 5) - 4) - 3) - 2) - 1 = 45

foldrl :: (a -> b -> a) -> a -> [b] -> a
foldrl step zero (x:xs) = step (foldrl step zero xs) x
foldrl _    zero []     = zero

Fold from the left of the list with the recursive term on the right

foldlr (-) 100 [1..10] = 10 - (9 - (8 - (7 - (6 - (5 - (4 - (3 - (2 - (1 - (100)))))))))) = 105

foldlr :: (a -> b -> b) -> b -> [a] -> b
foldlr step zero (x:xs) = foldlr step (step x zero) xs
foldlr _    zero []     = zero

Fold from the left of the list with the recursive term on the left

foldll (-) 100 [1..10] = ((((((((((100) - 1) - 2) - 3) - 4) - 5) - 6) - 7) - 8) - 9) - 10 = 45

foldll :: (a -> b -> a) -> a -> [b] -> a
foldll step zero (x:xs) = foldll step (step zero x) xs
foldll _    zero []     = zero

Only two of these folds made it into Prelude as foldr and foldl. Was there any reason to just include two folds, and why those two?

like image 307
martin Avatar asked Jul 02 '17 08:07

martin


People also ask

What are folds in your stomach?

The gastric folds (or gastric rugae) are coiled sections of tissue that exist in the mucosal and submucosal layers of the stomach. They provide elasticity by allowing the stomach to expand when a bolus enters it.

Is smooth brain a real thing?

Lissencephaly, which literally means "smooth brain," is a rare, gene-linked brain malformation characterized by the absence of normal convolutions (folds) in the cerebral cortex and an abnormally small head (microcephaly). In the usual condition of lissencephaly, children usually have a normal sized head at birth.

How long do children with lissencephaly live?

The life expectancy of lissencephaly is generally short. Many children with the condition die before they reach 10 years of age. The most common cause of death among people with lissencephaly is aspiration (breathing in a foreign object, such as sucking food into your airway) and respiratory disease.

What does Epicanthal folds mean?

An epicanthal fold is a skin fold of the upper eyelid covering the inner corner of the eye. It is often seen as a normal finding in very young children and is also common in people of Asiatic decent. An epicanthal fold can be an important diagnostic finding in conditions such as Down syndrome.


1 Answers

foldrl and foldlr don't add any expressive power: they are just the same as the other two folds but with the folding function flipped.

foldrl f = foldr (flip f)
foldlr f = foldl (flip f)

-- Or this, if you prefer
foldrl = foldr . flip
foldlr = foldl . flip

But it is not so easy to define foldl in terms of foldr, so providing them both is useful.

like image 149
amalloy Avatar answered Sep 22 '22 10:09

amalloy