Escolar Documentos
Profissional Documentos
Cultura Documentos
Simplified folding
foldl1
Examples
- Summation - foldl1 (+)
- Logical And - foldl1 (&&)
- Bananas - foldl1 mash
Associativity
foldl1 and foldr1
^ 3 ^
^ 3 ^
?
(2
^ 3) ^
4?
or
^ (3
^ 4) ?
Clue
Prelude> :i (^)
(^) :: (Num a, Integral b) => a -> b -> a
infixr 8 ^
-- Defined in GHC.Real
Clue
Prelude> :i (^)
(^) :: (Num a, Integral b) => a -> b -> a
-- Defined in GHC.Real
infixr 8 ^
^ 3) ^
4?
or
^ (3
^ 4) ?
4 ? = 4096
or
^ (3
^ 4) ?
NO!
4 ? = 4096
NO!
or
2 ^ (81) =
2417851639229258349412352 YES!
or according to numerals
Prelude Text.Numeral.Language.EN Text.Numeral.Grammar.Reified>
gb_cardinal defaultInflection (2^3^4)
Just "two septillion four hundred and seventeen sextillion eight hundred
and fifty-one quintillion six hundred and thirty-nine quadrillion two
hundred and twenty-nine trillion two hundred and fifty-eight billion three
hundred and forty-nine million four hundred and twelve thousand three
hundred and fifty-two"
Common associativities
Left
Right
+
*
/
`function`
^
**
:
++
&&
||
Fold left
Fold right
return type
return type
element
type
highest precedence opera
element
type
return type
Misnomers
How to remember
foldx :: ? -> ? -> ? -> Result
Function
Initial accumulator
Structure to fold
Heterogeneous Folding
foldl :: (b -> a -> b) -> b -> [a] -> b
return type is left argument