[Haskell-cafe] constant functions
Donald Bruce Stewart
dons at cse.unsw.edu.au
Wed Dec 27 23:06:41 EST 2006
nowgate:
> 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?
Yep. Which may be written as:
const :: a -> b -> a
> Is that reasoning OK? Are
>
> a -> (b -> a) and a -> b -> a the same signature?
Yep.
>
> So the inferred type is usually pretty accurate? These
Yes :-)
> 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>
let-bindings aren't supported in Hugs at the prompt, the solution is to
load the source from a file, with :reload, as you change it. You can
define them in GHC/GHCi however.
Also, you can use them locally as:
let always x _ = x in always 1 "foo"
-- Don
More information about the Haskell-Cafe
mailing list