How to replace a string with another in haskell -
i want replace string input file different string. searching method seems can alter string character character. illustration in code below
replace :: string -> string replace [] = [] replace (x:xs) = if x == '@' 'y':replace xs --y random char else x:replace xs searching :: string -> io string searching filename = text <- readfile filename return(replace text) main :: io () main = n <- searching "test.sf" writefile "writefile.html" n
i want find first occurrence of string "@title", cant seem find method mentioned before, can access char '@'. there method doing such task.
you can utilize data.list.utils replace, it's lazy , can process big file like:
main = getcontents >>= putstr . replace "sourcestring" "destinationstring"
that's all!
a possible replace function be
rep b s@(x:xs) = if isprefixof s -- then, write 'b' , replace jumping 'a' substring b++rep b (drop (length a) s) -- then, write 'x' char , seek replace tail string else x:rep b xs rep _ _ [] = []
another smart way (from data.string.utils)
replace :: eq => [a] -> [a] -> [a] -> [a] replace old new l = bring together new . split old $ l
haskell
No comments:
Post a Comment