Haskell: Syntax error in input (unexpected `=')

2.5k views Asked by At

I am trying to implement a function that compares 2 lists to see if they are the same. The syntax looks fine to me:

compare :: String -> String -> Bool

    compare [] [] = True -- error here
    compare (x,xs) (y,ys) = if x == y
        then compare xs ys
        else False

but I keep getting this error in the line marked above:

Syntax error in input (unexpected `=')

When I tried replacing '=' with '->', it worked fine but it gave the same error in the following line. So I did the same:

compare :: String -> String -> Bool

        compare [] [] -> True
        compare (x,xs) (y,ys) -> if x == y -- new error here
            then compare xs ys
            else False

But I got a different error:

Syntax error in type signature (unexpected keyword "if")

Now I really have no idea what's going on.

1

There are 1 answers

0
Sibi On BEST ANSWER

Your original first function is right except the fact that you are pattern matching it wrong. It should be like this:

compare (x:xs) (y:ys)    -- Not compare (x,xs) (y,ys)

Also as @ThreeFx suggested, do format your code properly. Ultimately it should look like this:

compare :: String -> String -> Bool
compare [] [] = True 
compare (x:xs) (y:ys) = if x == y
                        then compare xs ys
                        else False