Globally replace text in several files: Difference between revisions

→‎{{header|Haskell}}: Simplifed type of recursion in replace, preferred guards to if then else, applied Hlint
(Added Wren)
(→‎{{header|Haskell}}: Simplifed type of recursion in replace, preferred guards to if then else, applied Hlint)
Line 671:
</lang>
This other version is more effective because it processes the string more lazily, replacing the text as it consumes the input string (the previous version was stricter because of "matches" traversing the whole list; that would force the whole string into memory, which could cause the system to run out of memory with large text files).
<lang Haskell>replace _:: _Eq a => [a] =-> [a] -> [a] -> [a]
replace a b xx@(x:xs) = go
where
if isPrefixOf a xx
go [] = []
then b ++ replace a b (drop (length a) xx)
elsego xx@(x : replace a b xs)
then b ++ replace| a `isPrefixOf` xx = b <> go (drop (length a) xx)
</lang>
| otherwise = x : go xs</lang>
'''Example:'''
<pre>
9,655

edits