Concatenating a Custom Data Type List

advertisements

I'm building a set datatype in haskell and I am working on the remove function and I can't get it right, here is my code:

data Set a = Set [a] deriving (Eq,Ord,Show)

remove :: Integer -> Set Integer -> Set Integer
remove _ (Set []) = (Set [])
remove numberToRemove (Set (x:xs))
    |x == numberToRemove = Set(xs)
    |otherwise = Set(x:remove numberToRemove (Set xs))

I want to add x to the set remove is going to return but I do not know how to get it to work with my custom data type.

Here is my error:

test.hs:13:28:
Couldn't match expected type `[Integer]'
with actual type `Set Integer
In the return type of a call of `remove'
In the second argument of `(:)', namely
`remove numberToRemove (Set xs)'
In the first argument of `Set', namely
`(x : remove numberToRemove (Set xs))'
Failed, modules loaded: none.

Thanks


Use a where (or a let as it only affects one guard) to extract back the argument from Set

data Set a = Set [a] deriving (Eq,Ord,Show)   

remove :: Integer -> Set Integer -> Set Integer
remove _ (Set []) = (Set [])
remove numberToRemove (Set (x:xs))
    |x == numberToRemove = Set(xs)
    |otherwise = Set(x:y)
       where (Set y) = remove numberToRemove (Set xs)