haskell,file-io,lazy-evaluation , Haskell return lazy string from file IO


Haskell return lazy string from file IO

Question:

Tag: haskell,file-io,lazy-evaluation

Here I'm back again with a (for me) really strange behaviour of my newest masterpiece...

This code should read a file, but it doesn't:

readCsvContents :: String -> IO ( String )
readCsvContents fileName = do
     withFile fileName ReadMode (\handle -> do
          contents <- hGetContents handle
          return contents
          )

main = do
    contents <- readCsvContents "src\\EURUSD60.csv"
    putStrLn ("Read " ++ show (length contents) ++ " Bytes input data.")

The result is

Read 0 Bytes input data.

Now I changed the first function and added a putStrLn:

readCsvContents :: String -> IO ( String )
readCsvContents fileName = do
     withFile fileName ReadMode (\handle -> do
          contents <- hGetContents handle
          putStrLn ("hGetContents gave " ++ show (length contents) ++ " Bytes of input data.")
          return contents
          )

and the result is

hGetContents gave 3479360 Bytes of input data.
Read 3479360 Bytes input data.

WTF ??? Well, I know, Haskell is lazy. But I didn't know I had to kick it in the butt like this.


Answer:

You're right, this is a pain. Avoid using the old standard file IO module, for this reason – except to simply read an entire file that won't change, as you did; this can be done just fine with readFile.

readCsvContents :: Filepath -> IO String
readCsvContents fileName = do
   contents <- readFile fileName
   return contents

Note that, by the monad laws, this is exactly the same1 as

readCsvContents = readFile

The problem with what you tried is that the handle is closed unconditionally when the monad exits withFile, without checking whether lazy-evaluation of contents has actually forced the file reads. That is of course horrible; I would never bother to use handles myself. readFile avoids the problem by linking the closing of the handle to garbage-collection of the original result thunk; this isn't altogether nice either but often works quite well.

For proper work with file IO, check out either the conduit or pipes library. The former focuses a bit more on performance, the latter more on elegance (but really, the difference isn't that big).


1And your first try is the same as readCsvContents fn = withFile fn ReadMode hGetContents.


Related:


Hadoop map reduce Extract specific columns from csv file in csv format


java,hadoop,file-io,mapreduce,bigdata
I am new to hadoop and working on a big data project where I have to clean and filter given csv file. like if given csv file has 200 columns then I need to select only 20 specific columns (so called data filtering) as a output for further operation. also...

Why is f <$> g <$> x equivalent to (f . g) <$> x although <$> is not right-associative?


haskell,syntax,infix-notation,applicative,infix-operator
Why is f <$> g <$> x equivalent to (f . g) <$> x although <$> is not right-associative? (This kind of equivalence is valid in a popular idiom with plain $, but currently $ is right-associative!) <*> has the same associativity and precedence as <$>, but behaves differently! Example:...

How can I get the location of a user's Documents directory?


c#,file-io
I want to be able to write a string to a text file on any computer. For example string line = "Sample text to write" File.WriteAllText(@"C:\Users\Rolex James\Documents\sample.txt" , line); On another system this path would be different, is there a way of writing it such that you don't have to...

Cancel last line iteration on a file


python,python-3.x,for-loop,file-io
I need to iterate on a file, stop iteration on a condition and then continue parse the file at the same line with another function (That may change so I can't just add content in the previous function). An example file (file.txt) : 1 2 3 4 5 6 7...

Cannot access LocalFolder files on Windows phone app (UnauthorizedAccessException)


c#,file-io,windows-phone
I am trying to get an application to write (then read) a simple text file in Windows Phone 8. My app has three controls: a create file button, a display file button, and a textbox where the contents are supposed to display. I have the following events set up in...

Creating custom std::streambuf


c++,file-io,stl
I am defining a custom std::streambuf class for use in file I/O. In this class, I am overloading the functions open, close, is_open, xsgetn, xsputn, overflow, underflow, and uflow. I then insert this buffer into an std::ifstream as follows (the same scenario holds for std::ofstream): std::ifstream temp; Filebuffer *buffer =...

Refactor an IO recursive loop into a monad folding in Haskell


sockets,haskell,network-programming,io-monad
I writing a tcp server, and here's my main loop method: serverLoop :: Socket -> IO () serverLoop sock = do (conn, _) <- accept sock forkIO $ handleConn conn serverLoop sock (Note: handleConn :: Socket -> IO () is a function specific to my program.) I would like to...

Haskell IO - read from standard input directly to list


haskell
Haskell IO system is super hard to understand for me so i have question : How to read from standard input to list ? I know that there is function getLine :: IO String and interact. But i do not know how to convert the input to list so I...

Haskell - generate and use the same random list


haskell,random
In Haskell, I'd like to generate a random list of Ints and use it throughout my program. My current solution causes the array to be created randomly each time I access/use it. How can I overcome this problem?...

issues with installing newer cabal version for haskell vim now


ubuntu,haskell,vim,ubuntu-14.04,cabal
I would like to install this vim plugin: https://github.com/begriffs/haskell-vim-now When trying to run the suggested installation script: curl -o - https://raw.githubusercontent.com/begriffs/haskell-vim-now/master/install.sh | bash I get: --- Cabal version 1.18 or later is required. Aborting. I then try to install a newer version of cabal: [email protected]:~/Downloads/cabal-install-1.22.6.0$ ./bootstrap.sh The response I get:...

How to handle the File hand-off from windows in a python program


python,windows,pdf,file-io
I want to set up my python program to process all pdfs that are opened on my system, and then hand the processed pdf off to a standard reader. So I register my program with windows as the default handler for .pdf files and windows presumably will run my program...

Hook into GHC runtime system


haskell,functional-programming,runtime,transactional-memory
I have been looking at how transactional memory is implemented in Haskell, and I am not sure I understand how the STM operations exposed to the programmer hook into the runtime system functions written in C. In ghc/libraries/base/GHC/Conc/Sync.hs of the git repo, I see the following definitions: -- |A monad...

Combining Event and an attribute in threepenny-gui


haskell,threepenny-gui
I have an Event String which I want to sink into a textarea. This works fine, but now I want to combine the current value of a checkbox selection to this string. First I did this by using checkedChange of the checkbox which works, but has a problem. If the...

Replace all [ ] with {} - as short as possible [on hold]


haskell
Given the code below: import Data.List; main = (readLn :: IO [Integer]) >>= print . subsequences It takes a list of integers from standard input (for example [1,2,3]) and outputs something like: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] I want it to be like this: {},{1},{2},{1,2},{3},{1,3},{2,3},{1,2,3}} so my goal is to replace every [ and...

Implementing map on a tree using fold


scala,haskell
I am trying to implement a map using fold. I could do so in Haskell data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show) foldTree :: Tree a -> b -> (b -> a -> b -> b) -> b foldTree EmptyTree d _ =...

How do I avoid writing this type of Haskell boilerplate code


haskell,boilerplate
I run into this situation often enough for it to be annoying. Let's say I have a sum type which can hold an instance of x or a bunch of other things unrelated to x - data Foo x = X x | Y Int | Z String | ...(other...

Linux: Using split on limited space


linux,bash,file-io
I have a huge file on a linux machine. The file is ~20GB and the space on my box is ~25GB. I want to split the file into ~100mb parts. I know theres a 'split' command but that keeps the original file. I don't have enough space to keep the...

java.nio.file.NoSuchFileException: why nio not creating file [closed]


java,file-io,nio
I am using java.nio.file package and tried to create file with the following code. private static void printReport(String filename, String str)throws Exception{ ErrorCheck ec = new ErrorCheck(); String fileName = "/var/Emails_log/"+filename; Path filePath = Paths.get(fileName); File file = new File(fileName); final BufferedWriter out = Files.newBufferedWriter(filePath, StandardCharsets.UTF_8, StandardOpenOption.APPEND); try{ final Path...

How to “wrap” monadic return value


haskell,monads
I have the following code: data APNSIdentifier = NoIdentifier | Identifier Word32 deriving (Show, Eq) newtype APNSItem = Item Put createNotificationIdentifierItem :: APNSIdentifier -> APNSItem <--- here createNotificationIdentifierItem (Identifier identifier) = do putWord8 3 putWord16be 4 putWord32be identifier How can I "wrap" the Put monad with an APNSItem? Do I...

Idiomatic list construction


list,haskell,functional-programming,idiomatic
I'm very new to Haskell and functional programming in general, so I don't really know how to make this code idiomatic: type Coord = Double data Point = Point Coord Coord Coord deriving Show type Polyline = [Point] -- Add a point to a polyline addPoint :: Polyline -> Point...

Setting id and class with the haskell diagrams package


haskell,svg,haskell-diagrams
I am working with the diagrams package for haskell, and I am using the SVG backend. I embed the SVG markup directly into an HTML document, so that the graph as a part of a web page. I have built a pretty cool looking bar graph, and I would like...

Java FileOutputStream Created file Not unlocking


java,file-io
Currently am facing a problem with FileOutputStream in my code used FileOutputStream for creating file in my disk .Once file created there is no way for opening , deleting or moving file from its location ,getting error message already locked by other user When stopped web server it working properly...

Recursion scheme in Haskell for repeatedly breaking datatypes into “head” and “tail” and yielding a structure of results


haskell,recursion
In Haskell, I recently found the following function useful: listCase :: (a -> [a] -> b) -> [a] -> [b] listCase f [] = [] listCase f (x:xs) = f x xs : listCase f xs I used it to generate sliding windows of size 3 from a list, like...

Writing floats to a binary file in C++ | Equivalent of Java's DataOutputStream.writeFloat()


java,c++,file-io,binary
I'm porting some code from Java to C++ and need to write floats to a binary file. In Java I use DataOutputStream.writeFloat(). How do I do it in C++? I tried this method but it doesn't work: std::ofstream out; out.open(somePath, std::ios::out | std::ios::binary); float f = 0.5; out.write(reinterpret_cast<const char*>(&f), sizeof(float));...

Get each fibbonacci value in haskell


haskell,fibonacci
I'm learning haskell and I have the following code: fib a b = a : fib b (a + b) findFibSum = sum [x | x <- fib 1 2, mod x 2 == 0 && x < 100] If I run findFibSum nothing happens, it just sits there. Shouldn't...

Troubles reading a single character from a text file


java,file-io
I'm writing an "app" that takes in time input from the user and stores the hours and the minutes separately for each day in a text file (giving a result that looks like: day 1: 8h 45min day 2: 8h 43min ... ) the idea behind it is to use...

First three items of a list in Haskell


haskell
I am very new to Haskell, and struggling a bit with a function here. The premise is simple enough: Run through a list, and combine each 3 items next to each other with another function and return a list with the results. The problem is to do it in a...

Getting Apache Camel to stop retrying if failed to move the file after route completion


java,file-io,exception-handling,locking,apache-camel
The example route below picks up a file and performs a series of operations on them. Once completed, as noted in the camel:from field, the route is instructed to move the file to a .processed directory. Alternatively if failed, move it to the .error folder. The problem occurs when another...

logical expression evaluator Haskell


haskell
I wrote the following logical expression evaluator. It works for simple 2-member expressions, and it runs but produces a fault for expression containing other expressions as the second/first member. Here's my code. data Expression = Literal Bool | Operation Operator Expression Expression data Operator = AND | OR eval ::...

Haskell: When declaring a class, how can I use a type variable that is not immediately in the constructors?


haskell
I want to define a function, <-? to check whether an element is in a list/set/map. module Test where import qualified Data.Map as Map import qualified Data.Set as Set class Memberable a where (<-?) :: b -> a -> Bool instance Memberable [x] where (<-?) = elem instance Memberable (Map.Map...

Best practice for handling data types from 3rd party libraries in Haskell?


haskell,types
I'm just getting into my first real Haskell project of size (a web app), and I'm starting to run into issues with types from 3rd party libraries leaking all over my code. Here is a quick example: My Parser module imports Test.Parsec, and the exports a function (parseConfig) that returns...

Thread blocked indefinitely in an MVar operation


haskell,concurrency,network-programming
I have been attempting to debug a problem when using multiple MVars, however to no luck. My code uses two MVars: one to store the servers current state, and another to pass network events to and from the client threads. However after connecting and disconnecting several times, the server stops...

How to convert a Rational into a “pretty” String?


haskell,formatting,rational
I want to display some Rational values in their decimal expansion. That is, instead of displaying 3 % 4, I would rather display 0.75. I'd like this function to be of type Int -> Rational -> String. The first Int is to specify the maximum number of decimal places, since...

IO Monad Example


haskell
Consider the following IO code: ghci> let x = return 100 :: IO Int ghci> :t do { a <- x; print a; return 500 } do { a <- x; print a; return 500 } :: Num b => IO b My understanding of do notation/bind is that the...

Haskell do clause with multiple monad types


haskell,monads
I'm using a graphic library in Haskell called ThreePennyUI. In this library the main function returns a UI monad object. This causes me much headache as when I attempt to unpack IO values into local variables I receive errors complaining of different monad types. Here's an example of my problem:...

Haskell make recipe fails for Paradox theorem prover using GHC


linux,haskell,make,ghc,theorem-proving
I am trying to install the paradox theorem prover sourced from here. When I run the makefile this is the command that runs: ghc -optl -static -lstdc++ -I../instantiate -I../minisat/current-base ../minisat/current-base/Solver.or ../minisat/current-base/Prop.or ../instantiate/MiniSatWrapper.or ../instantiate/MiniSatInstantiateClause.or -fglasgow-exts -O2 -static -threaded -main-is Paradox.Main.main --make Paradox.Main -o paradox And it results in several errors like...

C++ basic fileIO locations


c++,osx,file-io
so I'm just starting to learn fileIO. I've been using a program called CodeRunner on my mac and I have a folder that contains: validsudoku.cpp, validsudoku, sudokugood0.txt The beginning of the code I wrote is: int main(int argc, char const *argv[]){ //string filetoopen; ifstream sudokutxtfile; string txtline; string sudokubox[9]; //bool...

Haskell return lazy string from file IO


haskell,file-io,lazy-evaluation
Here I'm back again with a (for me) really strange behaviour of my newest masterpiece... This code should read a file, but it doesn't: readCsvContents :: String -> IO ( String ) readCsvContents fileName = do withFile fileName ReadMode (\handle -> do contents <- hGetContents handle return contents ) main...

Stopping condition on a recursive function - Haskell


string,function,haskell,if-statement,recursion
So, I have this function which aims to align the text on the left without cutting words(only white spaces). However my problem is that I cannot find a stopping condition of the function and it goes infinitely. f n "" = "" --weak condition f n s = if n...

How do I load data from a txt file into variables in my C program?


c,arrays,parsing,file-io,struct
I have a program that has a struct of channels: struct channel { char title[40]; float gain; float offset; }; int main (int argc, char **argv) { struct channel channels[8]; } And a text file called configurationSettings.txt that holds the information needed to fill up 8 channels: Title1 20 30...

Keep track of loop without a counter


haskell
Say, I got a list which length can be odd or even. Every iteration , I remove two items from the list. If there is one or no item at the end, I end the execution. If I store (length list)/2 every loop, I will get e.g. [5,4,3...] for a...

skip method in CipherInputStream


java,file-io,cryptography,inputstream
I am running into an issue where the code that works against a InputStream backed by a FileInputStream does not work if a CipherInputStream is used. Example is below: // skipCount is same as n in a FileInputStream FileInputStream fis; ... skipCount = fis.skip(n) Get a different behavior if a...

Fold over a heterogeneous, compile time, list


haskell,type-level-computation,hlist
I have a list of heterogeneous types (or at least that's what I have in mind): data Nul data Bits b otherBits where BitsLst :: b -> otherBits -> Bits b otherBits NoMoreBits :: Bits b Nul Now, given an input type b, I want to go through all the...

Decremented value called in the recursion in Haskell


string,function,haskell,recursion,parameters
So, I have this function that aligns the text to the left for a given column width. I have already fixed some of its problems, but I am unable to make it not use the decremented parameter once it is called, but to return to the starting value of n....

How can I express foldr in terms of foldMap for type-aligned sequences?


haskell,types,monoids,type-variables,foldable
I'm playing around with type-aligned sequences, and in particular I'm messing around with the idea of folding them. A foldable type-aligned sequence looks something like this: class FoldableTA fm where foldMapTA :: Category h => (forall b c . a b c -> h b c) -> fm a b...

When I write to two files in python, the second one is always empty


python,file-io
I'm working on a simple python script to write some data to two text files. My code looks like this: with open('file1.txt', 'w') as file1: for thing in stuff1: file1.write(thing + '\n') with open('file2.txt', 'w') as file2: for thing in stuff2: file2.write(thing + '\n') When I run the program, file1...

apply a transformation with function inline


haskell
Starting from a simple case of "fold" (I used (+) but can be anything else): Prelude.foldl (+) 0 [10,20,30] is it possible apply an inline transformation similar to (that doesn't work): Prelude.foldl ((+) . (\x -> read x :: Int)) 0 ["10","20","30"] In case not, is there an alternative to...

populate a 'JTable' with values from a '.txt' file


java,swing,file-io,jtable,stringtokenizer
I'm new to java and I have a text file like this 0786160384|P. K.|Tharindu|912921549v|Colombo| 0711495765|P. K.|Gamini|657414589v|Colombo| 0114756199|H. P.|Weerasigha|657895478v|Kandy| I want to populate my 'jTable' with with the data from this text file. below is my code so far which doesn't work. When I execute the program nothing is displayed on...

How does Frege generalize number literals?


haskell,frege
It appears that Frege can evaluate 1/2 to return the Double value 0.5. The literal 1 is of type Int. It seems to be promoted to Double, which is a type in the Real class and thus knows the / operator. How does this happen? Is it using the Haskell...