Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  foldl by foldr (Brent Yorgey)


----------------------------------------------------------------------

Message: 1
Date: Fri, 14 May 2010 09:58:04 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] foldl by foldr
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

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
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 23, Issue 18
*****************************************

Reply via email to