Posted on March 2, 2014
by Kwang Yul Seo
Haskell is a non-strict language, and GHC uses a strategy called laziness which combines non-strictness and sharing for efficiency.
Thus, you can easily implement const which never uses the second argument.
const x y = x
With this definition, it is okay to pass undefined
as the second argument of const
because y
is not never evaluated. But in Haskell, you can also make an argument strict using the BangPatterns GHC extension.
const x !y = x
Interestingly, the situation is reversed in Scala whose default evaluation strategy is strict.
def const(x: Int, y:Int) = x
You can make an argument non-strict by putting the =>
symbol between the variable name and the type.
def const(x: Int, y: => Int) = x