Is there a nice way to find the first occurrence of a constructor in a list, without the explicit recursion in the sample below?
data Elem = A Int | B Char deriving Show
getA :: [Elem] -> Maybe Elem
getA [] = Nothing
getA (e:es) =
case e of
A a -> Just (A a)
_ -> getA es
Simply
Addendum: even better, future-proofed using an empty record pattern (kudos hammar):
Note however, that this only works out so neatly for matching constructors. For general properties,
findis nicer: