Number of arguments of composition in haskell

Viewed 54

Recently I have been learning about compositions in haskell and am now a bit confused concerning this example.

(const . min) 3 0 4

As a result I get 3, so internally it must be computed like:

const (min 3) 0 4

But I thought since min takes two arguments, it should look like this:

const (min 3 0) 4

So apparently the composition only takes this one argument, in this case 3, instead of all the arguments for min like I expected it to. Does that mean compositions only take one argument per default or what am I not getting here?

1 Answers

You can answer your question by manually evaluating the initial expression.

(const . min) 3 0 4
⇒ (\x -> const (min x)) 3 0 4    * defn of (.)
⇒ (const (min 3)) 0 4            * function application
⇒ ((\x y -> x) (min 3)) 0 4      * defn of const
⇒ (\y -> min 3) 0 4              * function application
⇒ (min 3) 4                      * function application
⇒ 3                              * because 3 is less than 4

It is worth noting that function application is left-associative, so, f x y means the same as (f x) y.

It is also worth noting that \x y -> z means the same as \x -> \y -> z.

Related