[Haskell-cafe] constant functions
Matthew Brecknell
haskell at brecknell.org
Thu Dec 28 02:32:28 EST 2006
> 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?
Indeed. In the type expression, the lower-case identifiers are type
variables, while the upper-case identifiers are types. Thus, "a" could
be instantiated to any type, with the constraint that both appearances
of "a" are the same type.
> map (complement odd) [1,2,3,4,5,6]
Typically, you would use function composition here:
map (not.odd) [1..6]
If you really want a seperate complement function, you could define it
using a section:
complement = (not.)
> 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?
Yes. Function application (->) is right-associative in a type
expression. What about a value expression?
f a b === (f a) b
Looks like an inconsistency? Not if you think about it. :-)
Of course, this is what curried functions and partial application are
all about.
> So the inferred type is usually pretty accurate? These
> signatures are a bit confusing. Is there a good
> tutorial?
http://haskell.org/tutorial, particularly chapter 2.
More information about the Haskell-Cafe
mailing list