[Haskell-cafe] What is the state if Unicode in Haskell implementations?

Bulat Ziganshin bulat.ziganshin at gmail.com
Mon Jul 31 08:51:42 EDT 2006

Hello Piotr,

Monday, July 31, 2006, 4:23:16 PM, you wrote:

>> first solution: http://haskell.org/haskellwiki/Library/Streams

> Looks nice. Just a quick question - does it have an equivalent of
> read(write)File?

no, but you can borrow this code from ghc's System.IO module, see
below. actually, if you need only these two functions, borrowing jhc's
code will be enough

-- | The 'interact' function takes a function of type @String->String@
-- as its argument.  The entire input from the standard input device is
-- passed to this function as its argument, and the resulting string is
-- output on the standard output device.

interact        ::  (String -> String) -> IO ()
interact f      =   do s <- getContents
                       putStr (f s)

-- | The 'readFile' function reads a file and
-- returns the contents of the file as a string.
-- The file is read lazily, on demand, as with 'getContents'.

readFile        :: FilePath -> IO String
readFile name   =  openFile name ReadMode >>= hGetContents

-- | The computation 'writeFile' @file str@ function writes the string @str@,
-- to the file @file at .
writeFile :: FilePath -> String -> IO ()
writeFile f txt = bracket (openFile f WriteMode) hClose
                          (\hdl -> hPutStr hdl txt) 

-- | The computation 'appendFile' @file str@ function appends the string @str@,
-- to the file @file at .
-- Note that 'writeFile' and 'appendFile' write a literal string
-- to a file.  To write a value of any printable type, as with 'print',
-- use the 'show' function to convert the value to a string first.
-- > main = appendFile "squares" (show [(x,x*x) | x <- [0,0.1..2]])

appendFile      :: FilePath -> String -> IO ()
appendFile f txt = bracket (openFile f AppendMode) hClose
                           (\hdl -> hPutStr hdl txt)

Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com

More information about the Haskell-Cafe mailing list