If you want to fold a list, I see four ways to do it.
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
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
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
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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With