I am trying to write a function subset
which takes two lists and determines whether the elements of the first appear in the second.
The code compiles to the GHCi but does not run (i.e. becomes stuck) when a function like the following is entered:
subset [1,2] [1,2]
This is my code:
subset :: (Eq a) => [a] -> [a] -> Bool
subset [] ys = True
subset (x:xs) ys
| elem x ys = subset (x:xs) ys
| otherwise = False
Thank you!
subset (x:xs) ys
| elem x ys = subset (x:xs) ys
-- ^^^^^^ --
Note that the recursive call above does not change the arguments! This will lead to infinite recursion. You want to remove x
before making the recursive call.