[Haskell-cafe] constant functions

Matthew Brecknell haskell at brecknell.org
Wed Dec 27 22:00:38 EST 2006


> This is what I've been trying:
> 
> always :: (a -> a) -> a -> a
> always x = (\y -> x)

Your function implementation is correct, but the type is wrong. Try
this:

always :: a -> b -> a

Or, just use the function "const", from the Prelude. :-)

The type system can be very handy when learning Haskell. If you think
you have the correct implementation but can't work out the type, just
start up an interpreter and ask it for the inferred type. For example:

Prelude> let always x _ = x
Prelude> :t always
always :: t -> t1 -> t

Once you have the type, ask Hoogle if the function already exists:

http://haskell.org/hoogle/?q=t+-%3E+t1+-%3E+t

And there is "const" at the top of the results. :-)




More information about the Haskell-Cafe mailing list