haskell-io

"Lazy IO" in Haskell?

I'm trying a little experiment in haskell, wondering if it is possible to exploit laziness to process IO. I'd like to write a function that takes a String (a list of Chars) and produces a string, lazily. I would like then to be abily to lazily feed it characters from IO, so each character would be processed as soon as it was available, a...

Wrong IO actions order using putStr and getLine

I have a code : main = do putStr "Test input : " content <- getLine putStrLn content And when I run it (with runhaskell) or compile it (ghc 6.10.4) result is like this: asd Test input : asd I'm new to haskell and in my opinion printing should be first. Am I right? In code sample on http://learnyouahaskell.com/ which use...

In Haskell, I want to read a file and then write to it. Do I need strictness annotation?

Hi, Still quite new to Haskell.. I want to read the contents of a file, do something with it possibly involving IO (using putStrLn for now) and then write new contents to the same file. I came up with: doit :: String -> IO () doit file = do contents <- withFile tagfile ReadMode $ \h -> hGetContents h putStrLn contents wit...

Multiple actions upon a case statement in Haskell

One last question for the evening, I'm building the main input function of my Haskell program and I have to check for the args that are brought in so I use args <- getArgs case length args of 0 -> putStrLn "No Arguments, exiting" otherwise -> { other methods here} Is there an intelligent way of setting up other methods, or is...

Haskell: read input character from console immediately, not after newline.

I've tried this: main = do hSetBuffering stdin NoBuffering c <- getChar but it waits until the enter is pressed, which is not what I want. I want to read the character immediately after user presses it. I am using ghc v6.12.1 on Windows 7. EDIT: workaround for me was moving from GHC to WinHugs, which supports this correctly...

Mapping over IO in Haskell

Is there a traditional way to map over a function that uses IO? Specifically, I'd like to map over a function that returns a random value of some kind. Using a normal map will result in an output of type ([IO b]), but to unpack the values in the list from IO, I need a something of type (IO [b]). So I wrote... mapIO :: (a -> IO b) -> [a]...

Problems decoding a file strictly with Binary in Haskell

Hi, I'm trying to read and decode a binary file strictly, which seems to work most of the time. But unfortunately in a few cases my Program fails with "too few bytes. Failed reading at byte position 1" I guess Binary its decode function thinks there is no data available, but I know there is and just rerunning the program works fine. ...

How to split a 110Mo file with Haskell

Hi I have a file which look like this index : label, index's value contain keys in the range of 0... 100000000 and label can be any String value, I want split this file which has 110 Mo in many slices of 100 lines each an make some computation upon each slice. How can I do this? 123 : "acgbdv" 127 : "ytehdh" 129 : "yhdhgdt" ... 989...

Limiting memory usage when reading files

I'm a Haskell beginner and thought this would be good exercise. I have an assignment where I need to read file in a thread A, handle the file lines in threads B_i, and then output the results in thread C. I have implemented this far already, but one of the requirements is that we cannot trust that the entire file fits into memory. I wa...

How to list directories faster?

I have a few situations where I need to list files recursively, but my implementations have been slow. I have a directory structure with 92784 files. find lists the files in less than 0.5 seconds, but my Haskell implementation is a lot slower. My first implementation took a bit over 9 seconds to complete, next version a bit over 5 secon...