[Haskell-cafe] Type error with simple list function

Bayley, Alistair Alistair_Bayley at invescoperpetual.co.uk
Thu Nov 8 07:52:18 EST 2007

> From: haskell-cafe-bounces at haskell.org 
> [mailto:haskell-cafe-bounces at haskell.org] On Behalf Of 
> This simple function definition that should rotate a list 
> (put the first 
> item in the last place) fails with a rather cryptic error with ghc:
> f :: [a] ->[a] ->[a]
> f (w : ws) = ws : w
> Couldn't match expected type `[a] -> [a]'
>      against inferred type `[[a]]'
> In the expression: ws : w
> In the definition of `f': f (w : ws) = ws : w

Well, there are a few problems. First, you've given a type sig which
suggests that f takes two arguments (both lists of type [a]) and returns
a list of type [a]. However, there is only one argument to f. A type sig
that better matches your function definition might be f :: [a] -> [a]

Second, the pattern matching in f (w:ws) matches the first item of the
list to w, and the rest of the list to ws. The type of the first item in
the list will be a, not [a].

Third, in the body of the function you're trying to join ws, which has
type [a], to w, which has type a. The (:) operator (AKA cons) expects
arguments of types a and [a] respectively i.e. the other way around from
what you have. I suspect you might want a different operator...

Hope this helps,
Confidentiality Note: The information contained in this message,
and any attachments, may contain confidential and/or privileged
material. It is intended solely for the person(s) or entity to
which it is addressed. Any review, retransmission, dissemination,
or taking of any action in reliance upon this information by
persons or entities other than the intended recipient(s) is
prohibited. If you received this in error, please contact the
sender and delete the material from any computer.

More information about the Haskell-Cafe mailing list