Difference between revisions of "UTF-8"
From HaskellWiki
(whoops, reuse the same example, why don't we) |
|||
Line 1: | Line 1: | ||
[[Category:Code]] | [[Category:Code]] | ||
− | The simplest solution seems to be to use the [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/utf8-string utf8-string package] from Galois. It provides a drop-in replacement for System.IO | + | The simplest solution seems to be to use the [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/utf8-string 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 <code>System.IO.UTF8</code>, we should also hide the equivalent one from the Prelude. (Alternatively, we could import the UTF8 module qualified) | ||
<haskell> | <haskell> | ||
− | import System.IO.UTF8 | + | > import System.IO.UTF8 |
− | import Prelude hiding ( | + | > import Prelude hiding (readFile) |
+ | </haskell> | ||
− | main = | + | <haskell> |
− | do | + | > main :: IO () |
− | + | > main = | |
− | + | > do args <- getArgs | |
− | </haskell> | + | > mapM_ reverseUTF8File args |
+ | |||
+ | > reverseUTF8File :: FilePath -> IO () | ||
+ | > reverseUTF8File f = | ||
+ | > do f <- readFile f | ||
+ | > writeFile (f ++ ".rev) $ reverseLines f | ||
+ | > where | ||
+ | > reverseLines = unlines . map reverse . lines | ||
+ | <haskell></haskell> |
Revision as of 09:28, 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
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)
> 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
<haskell>