[Haskell-beginners] foldl by foldr
Matt Andrew
mjsaand at gmail.com
Sun May 16 22:14:01 EDT 2010
Thanks everyone for all the help: I've now been able to understand the function and have a better understanding of partial application to boot.
Appreciate it,
Matt Andrew
At Fri, 14 May 2010 11:43:03 -0700,
John Bender wrote:
>
> [1 <multipart/alternative (7bit)>]
> [1.1 <text/plain; ISO-8859-1 (7bit)>]
> 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 <byorgey at 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
> > _______________________________________________
> > Beginners mailing list
> > Beginners at haskell.org
> > http://www.haskell.org/mailman/listinfo/beginners
> >
> [1.2 <text/html; ISO-8859-1 (quoted-printable)>]
>
> [2 <text/plain; us-ascii (7bit)>]
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
More information about the Beginners
mailing list