Home > database >  Return whether the given character is in a list or not without the use of the elem function and recu
Return whether the given character is in a list or not without the use of the elem function and recu

Time:11-14

I have to make the elementIsInList :: Eq a => a -> [a] -> Bool function that should behave the same way the elem function would behave, but I can't use recursion and of course, the elem function itself. I thought about implementing it using the filter function, but I can't figure out how filter works. Am I on the right track?

elementIsInList x xs = filter(x==xs) xs

CodePudding user response:

x == xs makes not much sense, x is an element to look for, whereas xs is the entire list of elements. You can make use of a lambda expression:

filter (\x -> x == y) xs

or of an infix operator section:

filter (x ==) xs

Furthermore you need to check if the list is empty. null :: Foldable f => f a -> Bool will return True if the list is empty, and False if it is not. You will then need to negate the result with not :: Bool -> Bool.

  • Related