Difference between revisions of "UTF-8"
Jump to navigation
Jump to search
Line 5: | Line 5: | ||
== Example == |
== Example == |
||
⚫ | |||
− | |||
− | This example reverses every line in a file (saving the results in the file + ".rev") |
||
− | |||
⚫ | |||
<haskell> |
<haskell> |
||
> import System.IO.UTF8 |
> import System.IO.UTF8 |
||
> import Prelude hiding (readFile, writeFile) |
> import Prelude hiding (readFile, writeFile) |
||
+ | > import System.Environment (getArgs) |
||
</haskell> |
</haskell> |
||
Line 22: | Line 20: | ||
> do args <- getArgs |
> do args <- getArgs |
||
> mapM_ reverseUTF8File args |
> mapM_ reverseUTF8File args |
||
+ | |||
− | |||
> reverseUTF8File :: FilePath -> IO () |
> reverseUTF8File :: FilePath -> IO () |
||
> reverseUTF8File f = |
> reverseUTF8File f = |
||
− | > do |
+ | > do c <- readFile f |
− | > writeFile (f ++ ".rev) $ reverseLines |
+ | > writeFile (f ++ ".rev") $ reverseLines c |
> where |
> where |
||
> reverseLines = unlines . map reverse . lines |
> reverseLines = unlines . map reverse . lines |
Revision as of 09:33, 21 July 2008
The simplest solution seems to be to use the utf8-string package from Galois. It
provides a drop-in replacement for System.IO
Example
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)
> import System.Environment (getArgs)
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 c <- readFile f
> writeFile (f ++ ".rev") $ reverseLines c
> where
> reverseLines = unlines . map reverse . lines