my top-level description technique failed me
I was trying to work problem 7.26 in Simon Thompson's "Craft of Functional Programming":
given a STRING search for SEARCH and replace with REPLACE
Now, having fallen in love with the ability to give top-down specs for things in Haskell, I tried this for starters:
substr search replace input = pre ++ sub ++ post
meaning given an input string, we have the unaltered pre and post parts and a conditionally altered sub part.
But I could not take this top-down description further without searching the string numerous times. John Goerzen was nice enough to come up with this solution for me:
{-
I'd recurse over the string, dropping the first character on
each recursion
then I'd compare the first (length search) characters to (search)
If they're the same, I'd return (replace ++ xs)
otherwise return x:self xs
-}
subst search@(s:ss) replace input@(i:is)
| chunkSearch search input = replace ++ (drop (length search) input)
| otherwise = i : subst search replace is
-- presumes length a < length b
chunkSearch a b = a == take (length a) b
Well, I like this solution, but I'm a bit worried that my top-down approach fell through on me. It's interesting how he simply builds the pre part on the fly and then pastes the post part in with replace
- metaperl's blog
- Login to post comments
If you wish all instances of the substring replaced, slightly modify the append:
++ subst(search replace (drop (length search) input))
Also, why the (s:ss) pattern match?
thanks for the input on global search and replace!
and yes, the (s:ss) is un-needed.
chunkSearch a b = and $ zipWith (==) a b --does not impose any requirements on the length ofaandb, only that they're not both infinite ;-)