Question

I try to get symmetric difference of two lists:

such as:

  [1,2,3] [2,3,5] 

the result is [1]

This is my code:

getlist _  []=[]
getlist [] _ = []
getlist (x:xs) ys = if isexist x ys
             then x++ getlist xs ys
                   else
                      getlist xs ys
isexist a [] = False
isexist a (x:xs) = if a == x
          then True
          else isexist a xs

There is an error:

The type variable 'a0' is ambiguous

Possible fix: add a type signature that fixes these type variable(s)

What does that mean? How can I fix it?

Was it helpful?

Solution

To fix the error all you have to do as @Joker_vD pointed out is to replace your ++ with :

To understand the difference between the two you should look at the type signature:

(:) :: a -> [a] -> [a]
(++) :: [a] -> [a] -> [a]

As you could see, : takes an element and a list and returns a list back. ++ on the other hand takes two lists.

You should look at Data.Set and Data.List packages if you want to do set intersections / diff more efficiently.

Note that after fixing the error, the program's output will not match what you wanted. You should investigate why that's happening and should be an easy fix.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top