Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expressing a sequence of scripted Python string replacement in Haskell

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.

like image 525
magnetar Avatar asked Aug 18 '26 04:08

magnetar


1 Answers

The simplest methods in Haskell involve mapping a Char -> Char replacement function (the f below) over the input, producing a new output (the interact function takes care of the fopen/fclose pattern):

main = interact $ map f
      where
        f '\n'   = ' '
        f '\r'   = ' '
        f 'é'    = 'e'
        f '\xa0' = ' '
        f c      = c

You can modify this to do your own IO, use the Text package etc, but the basic pattern of character transformations is the same.

like image 110
Don Stewart Avatar answered Aug 19 '26 19:08

Don Stewart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!