[Haskell-beginners] Preorder function application

Theodore Lief Gannon tanuki at gmail.com
Mon Jul 4 11:20:32 UTC 2016


If I understand correctly, you're asking why function application is
left-associative rather than right-associative. With currying and
first-class functions, right-association doesn't really work well. It's not
obvious with a highly-constrained function like "max", but in practice most
Haskell functions get composed and applied with other functions; real data
only matters at the endpoints. For a reasonably simple example, consider
the interaction between a couple of other Prelude functions:

map :: (a -> b) -> [a] -> [b]
const :: a -> b -> a

Since (b -> a) is a type all by itself, it fits into a type variable for
other functions; that is, "const" is an (a -> b) for the purposes of
filling the first argument of "map". Applying this gets:

map const :: [a] -> [b -> a]

This takes a list of values, and returns a list of functions. So
considering the type of "map const", which of the following behaviors seems
more appropriate:

> :t (map const) [1,2,3]
map const [1,2,3] :: Num a => [b -> a]

> :t map (const [1,2,3])
map (const [1,2,3]) :: Num a => [b] -> [[a]]

Haskell chooses the first option, because giving an argument [a] to an
expression whose type is currently [a] -> [b -> a] should probably return
[b -> a].

On Mon, Jul 4, 2016 at 12:43 AM, OxFord <fordforox at gmail.com> wrote:

> Hello,
>
> why Haskell doesn't apply functions in preorder?
> e.g.
>
> f x = x
>
> max 1 f 2
> > 2
> max f 1 f 2
> > 2
> max max f 1 f 2 f 3
> > 3
> f f f f f f f f f f f 1
> > 1
>
>
> Thus you would need to put the arguments into brackets only when you want
> to partially apply that function.
>
>
> Is the current method more readable or error prone?
>
> King regards,
>
> Ford
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160704/0698a844/attachment.html>


More information about the Beginners mailing list