質問

I'm wondering if a list is a prefix of a second list, using the following code:

prefix :: [a] -> [b] -> Bool
prefix [] _ = True
prefix _ [] = False
prefix (x:xs) (y:ys) = if (x==y) then prefix xs ys else False

But it returns an error:

Inferred type is not general enough
*** Expression    : prefix
*** Expected type : [a] -> [b] -> Bool
*** Inferred type : [a] -> [a] -> Bool

Can someone help me get this to work?

役に立ちましたか?

解決

Your type signature claims the two lists can have different types, but they can't. So the compiler complains that it inferred a type that was less general than the one you asked for.

他のヒント

That's because you're comparing the types you've named a and b using (x==y). Checking the type of ==, that means they are the same type, which has an equality test:

Prelude> :t (==)
(==) :: Eq a => a -> a -> Bool

So the type signature that is inferred is actually Eq a => [a] -> [a] -> Bool.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top