const function in Haskell

前端 未结 3 930
挽巷
挽巷 2020-12-15 16:48

The function const is defined in Prelude as:

const x _ = x

In GHCi, when I tried

Prelude> const 6 5  ->          


        
3条回答
  •  旧巷少年郎
    2020-12-15 17:09

    You seem to be thinking that this is equivalent to const (id 6) 5, where id 6 evaluates to 6, but it isn't. Without those parentheses, you're passing the id function as the first argument to const. So look at the definition of const again:

    const x _ = x
    

    This means that const id 6 = id. Therefore, const id 6 5 is equivalent to id 5, which is indeed 5.

提交回复
热议问题