Friday 29 April 2016

Haskell: const function

Prelude library defines const function like below.
const x _ = x

Whatever the second argument, const function always return the first argument of const function.


Example, We can implement length function like below.
Prelude> let length = sum.map(const 1)
Prelude> 
Prelude> length []
0
Prelude> length [1, 2, 3]
3
Prelude> length "Hello World"
11



Previous                                                 Next                                                 Home

No comments:

Post a Comment