On Fri, May 14, 2010 at 6:58 AM, Brent Yorgey
<byorgey@seas.upenn.edu> wrote:
On Fri, May 14, 2010 at 12:29:01PM +1000, Matt Andrew wrote:
>
> The thing I am having trouble understanding is what the 'id'
> function is doing in a function that expects 3 arguments and is
> given 4 (foldr).
"Number of arguments" in Haskell is a red herring. In fact, every
Haskell function takes exactly *one* argument. Functions which appear
to "take more than one argument" are really functions which take one
argument and return another function (which takes the next argument,
and so on). That's why the type of a "multi-argument" function is written like
X -> Y -> Z -> ...
which can also be written more explicitly as
X -> (Y -> (Z -> ...))
Polymorphic functions (like foldr) can also be deceiving as far as
"number of arguments" goes. For example, consider id:
id :: a -> a
Looks like this takes only one argument, right? Well, what if a = (Int -> Int):
id :: (Int -> Int) -> (Int -> Int)
which can also be written
id :: (Int -> Int) -> Int -> Int
so now it looks like id "takes two arguments" -- an (Int -> Int)
function, and an Int. Of course, the real answer is that id always
takes exactly one argument, just like any other function; but
sometimes that argument may itself be a function, in which case
the result can be applied to additional argument(s).
-Brent