Reputation: 51
I am trying to create function setEqual
.. This is what I have so far:
setEqual :: Eq a => [a] -> [a] -> Bool
setEqual [] _ = True
setEqual _ [] = False
setEqual a@(x:a') (y:b) | x == y = setEqual a' b
| otherwise = setEqual a b
setEqualTest = [ not ("ABCD" `setEqual` "ABC"),
not ("ABC" `setEqual` "ABCD"),
[0,2,1,3] `setEqual` [0,1,2,3] ]
When I run the test I get [True, False, False]
. Am I doing this correctly?
Upvotes: 1
Views: 482
Reputation: 22480
I think you just need to get two things right for writing your recursive function (to check set equality here): the base case and the recursive case.
You base case is incorrect. You don't know that an empty set []
is equal to an arbitrary set _
unless it's also empty. So it should be:
setEqual [] [] = True
plus that empty set is not equal to anything else:
setEqual [] _ = False
setEqual _ [] = False
(Note that the setEqual [] [] ...
line takes precedence as it is written before the others.
Your recursive case has a typo, and the last line should be:
| otherwise = setEqual a' b
Upvotes: 1