
Matt,
I was having issues with the very same problem not too long ago and I wrote
a blog post about how it illustrates the power of partial application.
http://nickelcode.com/2009/04/12/haskell-learnings/
Sorry for the blog plug, I just didn't see any reason to copy and paste the
content. I make no claims about the quality of my writing but it does
include some basic expansions of the execution/thunks for understanding.
Best
On Fri, May 14, 2010 at 6:58 AM, Brent Yorgey
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 _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners