This is a pretty simple implementation:
def elem[A](a: A, as: List[A]): Boolean = as match {
case Nil => false
case h :: t => (h == a) || elem(a, t)
}
Also, instead of find
you could use exists
:
def elem[A](a: A, as: List[A]) = as.exists(_ == a)
If you want to foldLeft
you can, but you'll still traverse the whole list instead of stopping early:
def elem[A](a: A, as: List[A]) =
as.foldLeft(false)((bool, value) => bool || value == a)