UTF-8

From HaskellWiki
Revision as of 09:30, 21 July 2008 by EricKow (talk | contribs)
Jump to navigation Jump to search


The simplest solution seems to be to use the utf8-string package from Galois. It provides a drop-in replacement for System.IO

Example

This example reverses every line in a file (saving the results in the file + ".rev")

If we use a function from System.IO.UTF8, we should also hide the equivalent one from the Prelude. (Alternatively, we could import the UTF8 module qualified)

> import System.IO.UTF8
> import Prelude hiding (readFile, writeFile)

The readFile and writeFile functions are the same as before...

> main :: IO ()
> main =
>  do args <- getArgs
>     mapM_ reverseUTF8File args
 
> reverseUTF8File :: FilePath -> IO ()
> reverseUTF8File f =
>   do f <- readFile f
>      writeFile (f ++ ".rev) $ reverseLines f
>   where
>     reverseLines = unlines . map reverse . lines