[Haskell-beginners] partial application

Keshav Kini keshav.kini at gmail.com
Mon Dec 3 22:10:53 CET 2012


Brent Yorgey <byorgey at seas.upenn.edu> writes:
> On Mon, Dec 03, 2012 at 03:20:41PM +0000, Miguel Negrao wrote:
>> 
>> A 03/12/2012, às 12:53, Brent Yorgey escreveu:
>> >> 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 
>> > Yes, a lambda is the only way to do it.
>> 
>> Given how compact haskell’s syntax can be, is there a reason why implementing such placeholders is not a good idea for haskell ?
>
> Yes: it would introduce horrible ambiguity.  Consider
>
>   (f (g _ 1 2))
>
> Does it mean
>
>   (f (\x -> g x 1 2))
>
> or
>
>   (\x -> f (g x 1 2))
>
> ?  I don't know of any good reason to prefer one over the other.  Any
> rule you come up with is likely to have other weird corner cases you
> hadn't considered.  As I understand it, Scala actually does have this
> feature, along with lots of confusing, ugly, ad-hoc rules to
> disambiguate situations like the above.

Another data-point: Mathematica also has this feature, if I correctly
understand the above posts [1]. The examples you gave would look like::

    (f (g _ 1 2)) ~= f[g[#1, 1, 2]]&

    (f (\x -> g x 1 2)) ~= f[g[#1, 1, 2]&]

    (\x -> f (g x 1 2)) ~= f[g[#1, 1, 2]]&

So it seems the ambiguity is resolved by having this scope-specifying
symbol '&'.

[1] http://reference.wolfram.com/mathematica/tutorial/PureFunctions.html

-Keshav




More information about the Beginners mailing list