[Haskell-cafe] foldl in terms of foldr

Alexander Solla ajs at 2piix.com
Tue Jan 26 17:20:57 EST 2010


On Jan 26, 2010, at 8:11 AM, Xingzhi Pan wrote:

> I'm a little confused with the type of step here.  Can it be
> considered as taking 2 or 3 arguments and then the compiler has to
> infer to decide?

The compiler is going to build up a sequence of functions.  Consider  
the following (mathematical) function:

f(x, y, z) = x^2 + y^2 + z^2.

This is a function in three arguments.  But if you bind any of them  
(say, x) to a value x', you end up with a function g(y,z) =  
f(x',y,z).  This is a function in two arguments.  Bind another  
variable, and you get another function, with one less argument.  You  
need to bind all the variables in order to compute f for a point.

>  Say if I, as a code reader, meet such a function
> defined with three formal parameters, how can I draw the conclusion of
> its type (and it takes 2 arguments actually)?


You can derive this from the syntactic properties of types.  Count the  
number of arrows that are not "in parentheses".  That's how many  
arguments the function takes.

f :: a -> b -> c is a function that takes an a, a b, and returns a c.

g :: (a -> b) -> c takes one argument, which is expected to be a  
function from a to b.  g returns a c.

That stuff I mentioned before about variable binding and function  
application still applies.  We can show that f and g have "isomorphic"  
types.  But they are conceptually different types.



More information about the Haskell-Cafe mailing list