The function const is defined in Prelude as:
const x _ = x
In GHCi, when I tried
Prelude> const 6 5 -> Gives 6
But when I tried
Prelude> const id 6 5 -> Gives 5
Even after making changes like
Prelude> (const id 6) 5 -> Gives 5
Shouldn’t this function give 6 as the output of function id has type id :: a -> a which should bind like
Prelude> (const 6) 5 -> Gives 6
Why does function const behave differently?
You seem to be thinking that this is equivalent to
const (id 6) 5, whereid 6evaluates to 6, but it isn’t. Without those parentheses, you’re passing theidfunction as the first argument toconst. So look at the definition ofconstagain:This means that
const id 6 = id. Therefore,const id 6 5is equivalent toid 5, which is indeed 5.