
Hi, 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 What's Haskell trying to tell me? I'm a newby so please forgive my ignorance. Thanks! Fernando

2007/11/8, Fernando Rodriguez
Hi,
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
What's Haskell trying to tell me? I'm a newby so please forgive my ignorance.
Hi, as haskell newbie I will try to explain. Launch ghci and write: Prelude> :t (:) (:) :: a -> [a] -> [a] Now compare with types you got. What you want is probably something like this: f :: [a] ->[a] ->[a] f (w : ws) = ws ++ [w] Prelude> :t (++) (++) :: [a] -> [a] -> [a] but I believe this is highly unefficient. You may also want to experiment with tail, head and reverse to achieve what you want. But others will probably tell you better ways to do it. Cheers, Radek. -- Codeside: http://codeside.org/ Przedszkole Miejskie nr 86 w Lodzi: http://www.pm86.pl/

From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@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, Alistair ***************************************************************** 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. *****************************************************************

Hola Fernando,
On Nov 8, 2007 1:52 PM, Bayley, Alistair
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]
Alistair is surely aware of this, but, as a side note, and even if in this concrete case f might need to make the two arguments explicit, currying permits avoid specifing all the arguments (and it's used all the time by Haskell programmers) This two definitions are equivalent add1 :: Num a => [a] -> [a] add1 = map (+1) add1 :: Num a => [a] -> [a] add1 xs = map (+1) xs

Fernando Rodriguez
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
What's Haskell trying to tell me? I'm a newby so please forgive my ignorance.
First that your type signature doesn't match the definition - you claim in the signature that f will take two parameters, but you give it only one (a list) in the definition. That wouldn't be illegal if the right hand side was a function, but it isn't. When you fix that, Haskell will go on to tell you something about the type of (:). :-) -k -- If I haven't seen further, it is by standing in the footprints of giants
participants (5)
-
Alfonso Acosta
-
Bayley, Alistair
-
Fernando Rodriguez
-
Ketil Malde
-
Radosław Grzanka