[Haskell-cafe] Am I doing it right?

Creighton Hogg wchogg at gmail.com
Wed Sep 24 14:14:31 EDT 2008


Hi Haskellers,
So as another step in my education, I wanted to ask if the following
code at least feels 'morally' correct as a find/replace function,
replacing each occurrence of the sub-list before with after in the
list.  unfoldr felt like the most natural higher order function to
capture the recursion, but I imagine there are still slicker ways to
have done this because I am creating an intermediate list-of-lists.
However, doing it this way seems at least moderately speedy given that
I was able to read in, find/replace, and write out a 120 MB file in 30
seconds on a fairly dinky linux box.

replace :: (Eq a) => [a] -> [a] -> [a] -> [a]
replace before after = concat . unfoldr aux
     where aux [] = Nothing
               aux str | take l str == before = Just (after,drop l str)
                           | otherwise = Just (take 1 str,drop 1 str)
               l = length before

Thanks,
Creighton


More information about the Haskell-Cafe mailing list