[Haskell-beginners] partial application

Peter Hall peter.hall at memorphic.com
Wed Dec 5 03:47:31 CET 2012


> >
> > g = (\x -> f x 1 2 3)
>
> Yes, a lambda is the only way to do it.

This is not exactly the same thing, but if you have a function that accepts
only two arguments, you can use `flip` to partially apply with the other
one:

f a b = a ++ b
g = (flip f) "xyz"

g "abc" -- "abcxyz"

To extend that to functions with more parameters, you'd have to create an
equivalent of `flip` for each arity, and the desired permutations might be
more complicated than just reversing them. Still, in the two-argument case,
flip can often be cleaner than a lambda.

Peter


On 3 December 2012 12:53, Brent Yorgey <byorgey at seas.upenn.edu> wrote:

> On Mon, Dec 03, 2012 at 12:28:08PM +0000, Miguel Negrao wrote:
> > Hi list,
> >
> > Is there a syntax in Haskell for partial application that would be
> something like this the following ?
> >
> > f a b c d  = a +b+ c+ d
> >
> > g = f _ 1 2 3
> >
> > or the only way to do it is like below ?
> >
> > g = (\x -> f x 1 2 3)
>
> Yes, a lambda is the only way to do it.
>
> >  Also, hlint complains about top-level functions without type even
> > if they are not exported out of the corresponding module. Is is
> > really bad style not put type signatures in all top-level functions
> > ? I really like the fact that haskell takes care of the type
> > signatures for me.
>
> Yes, it is bad style.  Let me give you two reasons why I always
> encourage putting type signatures on all top-level functions.  Whether
> they are exported or not really makes no difference.
>
>   1. Writing the type signature for a function *first*, before
>      implementing it, really helps a LOT in clarifying things in your
>      own mind.  If you cannot write down the intended type of a
>      function then you do not understand what it is supposed to do.
>      If you do not understand what it is supposed to do then how do
>      you expect to be able to implement it?
>
>   2. Suppose you make a mistake when implementing a function.  If you
>      don't give a type signature, it's possible that GHC will infer
>      some type for it anyway (but not the type you intended!).  Now
>      you will not get an error until further down the line when you
>      use that function somewhere else.  And what's more, the error
>      will not tell you anything about the real problem.  It will just
>      say "X does not match Y on line 297" and you will have to do a
>      lot of work to figure out that the real problem is that you
>      messed up in implementing function foo on line 43.  But if you
>      had put a type signature on foo in the first place, you would
>      have gotten an error immediately.
>
> -Brent
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121205/98eeb691/attachment.htm>


More information about the Beginners mailing list