Recursion or list comprehension?

Viewed 4047

Working through Learn You a Haskell For Great Good, in the chapter on higher-order functions the author walks through an implementation of a few different library functions. When coming to the definition of filter' (a reimplementation of the standard library function filter), I thought that the obvious thing was this:

filter' f xs = [x | x <- xs, f x]

But the author gives the following longer, recursive definition:

filter' _ [] = []  
filter' p (x:xs)   
    | p x       = x : filter' p xs  
    | otherwise = filter' p xs

Both definitions do the same thing. Is there any reason for this? Is the recursive definition somehow more performant? Is it more idiomatic for Haskell? Something else?

2 Answers
Related