[Haskell-cafe] constant functions

michael rice nowgate at yahoo.com
Wed Dec 27 22:55:11 EST 2006


Thanks! I figured I was close.

Didn't even know const was available.

I put together a compliment functions earlier

complement :: (a -> Bool) -> a -> Bool
complement p x =  not (p x)

By the signature, the first argument is a function
(predicate) which when given a value returns a Bool?
And the second argument is just a value? And the
function returns a Bool?

> map (complement odd) [1,2,3,4,5,6]
[False,True,False,True,False,True]
> 

By similar reasoning the always function would seem to
have a signature

a -> (b -> a)

where the first argument is just a value and the
return value is a function that when given a possibly
different value just returns the value originally
given to always?

Is that reasoning OK? Are

a -> (b -> a) and a -> b -> a the same signature?

So the inferred type is usually pretty accurate? These
signatures are a bit confusing. Is there a good
tutorial?

I'm using Hugs/Win XP just to scope out the language
right now. I tried what you suggested and got

Hugs> let always x _ = x
ERROR - Syntax error in expression (unexpected end of
input)
Hugs>

Isn't Hugs an interpreter?

Thanks again. Really interesting language Haskell.

Michael

--- Matthew Brecknell <haskell at brecknell.org> wrote:

> > 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. :-)
> 
> 
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


More information about the Haskell-Cafe mailing list