Is the function I give to foldl
applied in an infix way?
Example
foldl (-) 0 [1,2,3]
= 0-1-2-3
= -6
so more generally:
foldl f x [a,b,c]
is applied as:
(((x `f` a) `f` b) `f` c)
I know it's recursive, but can I think about it that way?
The only difference between infix function application and prefix function application is syntax, so your question does not make very much sense. Outside of referring to the syntax of a particular expression, applying a function “in an infix way” doesn’t mean anything.
In Haskell, when you write x + y
, it is precisely equivalent to writing (+) x y
. Likewise, x `op` y
is precisely equivalent to writing op x y
. Put another way, application of an infix operator is still just plain old function application where the function is applied to two arguments.
If it helps you to visualize foldl
via an expression like ((a `f` b) `f` c) `f` d
instead of one like f (f (f a b) c) d
, that’s certainly within your right, since the two expressions are equivalent. Indeed, the documentation for foldl
uses infix notation to help explain the function’s behavior, since it is a useful representation that helps get the point across. But be careful not to confuse notation (aka syntax) with denotation (aka meaning). Many programs can be notationally distinct but denotationally equivalent.