[Haskell-cafe] Re: [Haskell] Trying to get a Composite design pattern to work

Jared Updike jupdike at gmail.com
Mon Mar 13 17:37:11 EST 2006

(Moved to haskell-cafe)

> Actually, I'm trying to avoid library functions, so I can learn the
> language and the functional way of thinking.  How would one implement
> the concatMap function?

The Haskell 98 report gives possible implementations of standard functions:


foldr            :: (a -> b -> b) -> b -> [a] -> b
foldr f z []     =  z
foldr f z (x:xs) =  f x (foldr f z xs)

map :: (a -> b) -> [a] -> [b]
map f []     = []
map f (x:xs) = f x : map f xs

concat :: [[a]] -> [a]
concat xss = foldr (++) [] xss

concatMap :: (a -> [b]) -> [a] -> [b]
concatMap f = concat . map f


The book Haskell School of Expression (by Paul Hudak) goes over using
recursion as a control structure and then shows you how to replace
your simple recursion patterns with library functions once you start
to see these general patterns. As you start to understand the
functional style of iterating over lists, you should begin to see when
to use the library functions, and how it will save you time and be
more simple and clear than using recursion directly for every

If you want a place to start, I would challenge you to expand the
concatMap definition above with the definitions of concat and map, and
then plug in the definition of foldr and see if you can make your own
concatMap function. Maybe that will help you understand things better.

Hope that helps,
reverse ")-:"

More information about the Haskell-Cafe mailing list