Re[2]: Beginner help
antonmuhin на rambler.ru
antonmuhin на rambler.ru
Tue, 11 Feb 2003 20:17:49 +0300
Dear Haskellers!
Another letter from newbie :)
May you criticize my code for replacing substring in a string cited
below? Any critics and comments are highly appreciated.
And some more questions:
1. Is there more functional way for text processing? For example,
intuitively I feel that there is more functional approach for matching
than one suggested below.
2. Is there a kind of book simlar to "Text processing with Python?".
3. I found several regular exprssions library for Haskell. Which one
would you recommend for *learning*---I am not too interested in
performance by now, but rather in exampels of good Haskell.
4. What Haskell sources (snippets, modules, etc) can you recommend as
examples of good Haskell.
Replace:
data Match = None | Rest String deriving Show
match :: String -> String -> Match
match pattern s = case match_ pattern s of
("", rest) -> Rest rest
_ -> None
where match_ :: String -> String -> (String, String)
match_ pattern@(p:ps) str@(s:ss) = if p == s then match_ ps ss else (pattern, str)
match_ pattern s = (pattern, s)
findAll :: String ->String -> (String -> String) -> String
findAll pattern = findAll_
where findAll_ :: String -> (String -> String) -> String
findAll_ "" _ = ""
findAll_ s@(head:tail) f = case match pattern s of
Rest rest -> (f pattern) ++ findAll_ rest f
None -> head:findAll_ tail f
replace :: String -> String -> String -> String
replace _ "" _ = ""
replace what s to = case match what s of
Rest rest -> to ++ replace what rest to
None -> (head s):(replace what (tail s) to)
replace3 what s to = findAll what s (\_ -> to)
Notes:
It seems that Match may be better written with Maybe. is there any
pros & contros? Which version of replace would you prefer and why? Is
it effecient enough or there are better ways?
I beg your pardon for such a long letter.
--
Best regards,
antonmuhin mailto:antonmuhin@rambler.ru