Take the 2-minute tour ×

I am new to Haskell, sorry if this is a basic question.

I currently have a list of Int's and I am trying to create a function that takes a variable x and returns a boolean depending whether the variable exists in the list.

I have had a search and found Data.List find function but this dosent seem to return a boolean.

I am using GHCi.


share|improve this question

3 Answers 3

up vote 32 down vote accepted

First find the type of the function you need.

To "Check if" means to return either True or False, a Bool.

So the function takes an Int, a list of Int (aka [Int]) and returns Bool:

Int -> [Int] -> Bool

Now ask hoogle.


Hoogle is a very useful tool. You can integrate it with ghci.

share|improve this answer

If the standard elem function didn't exist, you could have been on the right track with find.

myElem x = maybe False (const True) . find (== x)

There's lots of other ways to implement it too, like

myElem x = any (== x)
myElem x = or . map (== x)
myElem x = not . null . filter (== x)
myElem x = foldr (\y b -> y == x || b) False


share|improve this answer

i did it more simply like this.


checkIfElem :: Int -> [Int] ->Bool
checkIfElem x l 
         |x`elem` l =True
share|improve this answer
That is indeed simple, but one could also say it's an incredibly complicated way of writing the definition checkIfElem = elem, because it's exactly equivalent to that! (Guards already check on boolean values, | condition = True | otherwise = False therefore is equivalent to simply = condition. That leaves you with checkIfElem x l = elem x l, where by η-reduction you can drop the arguments on both sides.) –  leftaroundabout Nov 22 '13 at 12:28

Your Answer


