2008/7/22 Ronald Guida <[EMAIL PROTECTED]>:
> 2008/7/22 Dmitri O.Kondratiev <[EMAIL PROTECTED]>:
>> On the side: The more I use Haskell - the more I like it ! It helps me think
>> about the problem I solve much more clearly then when I use imperative
>> language.
>
> If I want to replace a substring in a string, then I would search my
> string left to right, looking for any occurrence of the substring. If
> I find such an occurrence, I would replace it and continue searching
> from immediately after the replacement. This algorithm can be
> directly expressed in Haskell. More efficient algorithms do exist.
Your idea but expressed in a more elegant fashion (maybe...) :
replace :: (Eq a) => [a] -> [a] -> [a] -> [a]
replace _ _ [] = []
replace old new xs@(y:ys) =
case stripPrefix old xs of
Nothing -> y : replace old new ys
Just ys' -> new ++ replace old new ys'
--
Jedaï
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe