foldl . foldr function composition - Haskell
fold, function-composition, haskell
Solution
(foldl.foldr) (+) 1 [[1,2,3],[4,5,6]]
becomes
foldl (foldr (+)) 1 [[1,2,3],[4,5,6]]
So you get
foldl (foldr (+)) (foldr (+) 1 [1,2,3]) [[4,5,6]]
after the first step of `foldl`, or
foldl (foldr (+)) 7 [[4,5,6]]
if we evaluate the applied `foldr` (unless the strictness analyser kicks in, it would in reality remain an unevaluated thunk until the `foldl` has traversed the entire list, but the next expression is more readable with it evaluated), and that becomes
foldl (foldr (+)) (foldr (+) 7 [4,5,6]) []
and finally
foldl (foldr (+)) 22 []
~> 22
Problem
So, I'm really frying my brain trying do understand the foldl.foldr composition. Here is a example: ``` (foldl.foldr) (+) 1 [[1,2,3],[4,5,6]] ``` The result is 22, but what's really happening here? To me it looks like this is what is happening: `foldl (+) 1 [6,15]`. My doubt is related to the `foldr` part. Shouldn't it add the 1 to all the sub-lists? Like this: `foldr (+) 1 [1,2,3]`. In my head the 1 is added just one time, is it right? (probably not, but I want to know how/why!). I'm very confused (and perhaps making all the confusion, haha). Thank you!