[Haskell-beginners] partial application

Brent Yorgey byorgey at seas.upenn.edu
Mon Dec 3 13:53:58 CET 2012


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



More information about the Beginners mailing list