const¶
Type signature for const
:
const :: ∀ a b. a -> b -> a
The type signature tells us that const
is a function that takes two fully polymorphic type parameters and always returns the first parameter, untouched.
const v1¶
const :: ∀ a b. a -> b -> a
const x _ = x
const v2¶
const :: ∀ a b. a -> b -> a
const = \x _ -> x
const v3¶
f3 :: ∀ a b. a -> (b -> a)
f3 x = \_ -> x
const usage¶
with filter¶
r2 = filter (const true) [1, 2, 3, 4]
filter
calls const true 1
, then const true 2
, etc. for each element of the array. const
will always ignore the element, and only consider true
(the first argument, since const
always ignores the its second argument).
Observe that we partially apply const
to true
, so, each element of the list is in turn, passed as the remaining, “missing” (second) argument to const
, which is promptly always ignored. The result is that the predicate is always satisfied and all elements are kept in the resulting array.