Search code examples
haskelltypeslist-comprehensiontype-signature

Haskell "Couldn't match expected type ‘a’ with actual type ‘[a0]’"


Im doing a project in Haskell where I am trying to create a function which takes two list inputs and then returns a union of the list but without any duplicates.

The problem is that I keep getting the error message:

Couldn't match expected type ‘a’ with actual type ‘[t0]’
      ‘a’ is a rigid type variable bound by
          the type signature for newList :: [a] -> [a] -> [a]

Here is my code:

allList :: (Eq a) => [a] -> [a] -> [a]
allList [] [] = []
allList x y = (x ++ y)

checkDup [] = []
checkDup (z:zs)
    | z `elem` zs = checkDup zs
    | otherwise = z : checkDup zs

newList :: (Eq a) => [a] -> [a] -> [a]
newList [] [] = []
newList x y = [checkDup z | z <- allList x y]

The first allList function creates a list of the two list, the checkDup creates a new list without any duplicates and the newList uses list comprehension to pass the combined list to the checkDup.

Anyone know where I am going wrong?


Solution

  • The problem lies here:

    newList x y = [checkDup z | z <- allList x y]
    

    z is supposed to be a list you pass to checkDup, but in this case, z is just a single element

    Maybe you want:

    newList x y = checkDup $ allList x y
    

    newList can be declared as follows:

    newList :: (Eq a) => [a] -> [a] -> [a]
    newList = checkDup . allList