I am trying to understand laziness and seq in Haskell:
in (1), is it correct that no evaluation of
voccurs until the print in the base case requiresv?in (2), is it correct that
v'is evaluated before each recursive call, so that no evaluation ofvis needed in the base case? If not, how do I implement this strict evaluation?are there any profiling tools I can use to confirm these two points for myself?
main = do
f [1, 2, 3, 4] 0
f' [1, 2, 3, 4] 0
g x = 42 * x -- this could be whatever
-- 1. lazy
f [] v = print v -- base case
f (x:xs) v = f xs v'
where v' = v + g x
-- 2. strict
f' [] v = print v -- base case
f' (x:xs) v = seq v' f' xs v'
where v' = v + g x
I know foldl and foldl' do what I want in these cases. I am more interested in understanding how this is implemented.