[Haskell-cafe] Type error with simple list function
Radosław Grzanka
radoslawg at gmail.com
Thu Nov 8 07:48:17 EST 2007
2007/11/8, Fernando Rodriguez <frr149 at easyjob.net>:
>
> 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/
More information about the Haskell-Cafe
mailing list