: , , Haskell - ?
GHC , . , ", " . , , foldr foldl:
foldr (+) 0 (1:2:3:[])
1 + foldr (+) 0 (2:3:[])
1 + (2 + foldr (+) 0 (3:[]))
1 + (2 + (3 + foldr (+) 0 [])))
1 + (2 + (3 + 0))
1 + (2 + 3)
1 + 5
6
, foldr , , . , (+) , , . (+) , , .
, foldl - , ?
foldl (+) 0 (1:2:3:[])
foldl (+) (0+1) (2:3:[])
foldl (+) ((0+1)+2) (3:[])
foldl (+) (((0+1)+2)+3) []
((0+1)+2)+3
(1+2)+3
3+3
6
. -, foldl , , . (+) . , , " " GHC.
, , thunks, , foldl ( sum) Don), thunks (+)? foldl' :
foldl' (+) 0 (1:2:3:[])
foldl' (+) 1 (2:3:[])
foldl' (+) 3 (3:[])
foldl' (+) 6 []
6
, .
In conclusion, if your recursive calls remain the largest, most external (which corresponds to the position of the tail), they can be reduced with a lazy score. This is necessary but not sufficient to prevent the evaluation of your recursive function from using the O (n) stack and heap space. foldland foldrstyle recursion themselves take an O (n) stack and a bunch of space. foldl-line recursion with strict annotations on cumulative parameters is necessary for evaluating work in a constant space.