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:  partial application (Peter Hall)


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

Message: 1
Date: Wed, 5 Dec 2012 02:47:31 +0000
From: Peter Hall <[email protected]>
Subject: Re: [Haskell-beginners] partial application
To: Brent Yorgey <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID:
        <CAA6hAk4d-PxXv=cf1sz8bnhrzsvwm_y+nnhqd8cupqof0ds...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

> >
> > 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 <[email protected]> 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
> [email protected]
> 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-0001.htm>

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

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 54, Issue 7
****************************************

Reply via email to