[Haskell-cafe] Prolog-style patterns

Jan Stolarek jan.stolarek at p.lodz.pl
Mon Apr 8 13:11:09 CEST 2013


Hi all,

consider this simple reimplementation of 'elem' function:

member :: Eq a => a -> [a] -> Bool
member _ [] = False
member y (x:xs) | x == y    = True
                | otherwise = member y xs

If Haskell allowed to write pattern matching similar to Prolog then we could write this function 
like this:

member :: Eq a => a -> [a] -> Bool
member _ []     = False
member x (x:_)  = True
member x (_:xs) = member x xs

The meaning of pattern in the second equation is "match this equation if the first argument equals 
to head of the list". Many times I have found myself instinctively writing patterns in this way, 
only to get a compilation error. I was thinking about implementing language extension for GHC 
that would allow to write Prolog-style patterns. Would there be an interest in such an extension? 
Also, if I missed something obvious please let me know.

Janek



More information about the Haskell-Cafe mailing list