I often use Python to replace various types of characters in text, using scripts that look like this:
#!/usr/bin/env python
# coding=UTF-8
import sys
for file in sys.argv[1:]:
f = open(file)
fs = f.read()
r1 = fs.replace('\n',' ')
r2 = r1.replace('\r',' ')
r3 = r2.replace('. ','.\n\n')
r4 = r3.replace('é','e')
r5 = r4.replace('\xc2',' ')
r6 = r5.replace('\xa0',' ')
r7 = r6.replace(' ',' ')
r8 = r7.replace(' ',' ')
r9 = r8.replace('\n ','\n')
f.close()
print r8
But I am learning Haskell now, because I am sick of Python.
My best try at doing that in Haskell is
#!/usr/bin/runhaskell
import System.IO
main :: IO ()
main = do
inh <- getArgs >>= withFileLines
outh <- -- ??
mainloop inh outh
hClose inh
hClose outh
replacements :: String -> String
replacements = unwords $ map -- hmm....
…and, I have no idea where to go from there.
The simplest methods in Haskell involve mapping a
Char -> Charreplacement function (thefbelow) over the input, producing a new output (theinteractfunction takes care of the fopen/fclose pattern):You can modify this to do your own IO, use the
Textpackage etc, but the basic pattern of character transformations is the same.