1
votes

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
All Haskell functions take one argument. E.g. min 3 0 applies the (1-arg) function min to argument 3. The result is a (1-arg) function, that is then applied to 0. The result of that is the number 0. We of course like to consider min as a 2-arg function, but technically there's no such a thing in Haskell. Coherently, composition composes two (1-arg) functions. To compose "2-arg" functions (i.e., 1-arg returning 1-arg functions) one would need some other operator, or simply use a lambda. - chi

1 Answers

4
votes

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.