I’m trying to implement the elem function recursively. This is what I wrote:
member :: Eq a => a -> [a] -> Bool
member _ [] = False
member n (x:xs)
| n == x = True : member (n xs)
| otherwise = False
main = do
print (member 10 [1,12,11])
I got a ‘couldn’t match expected type ‘Bool’ with actual type ‘[a0]’ error.
I attempted the same using if..else..then as well, but in vain.
I think I’m missing out on a very basic and fundamental concept of Haskell here.
Help?
The clause
True : member (n xs)does not match withmember‘s declared return typeBool. If you find anxinxswithx == n, then you want to simply returnTrue.Otherwise, you should recurse in
memberwith a smaller listxs(i.e., check again for equality ofnwith the next element). Here is your code with these two problems fixed as described: