← Back to context

Comment by jroesch

14 years ago

I was once one of the people who was incorrectly told, and therefore believed simple tasks like IO in Haskell was hard. Though it couldn't be further from the truth.

    main = do handle <- openFile "filename.txt" ReadMode
              contents <- hGetContents h
              putStr contents

Now that simple bit opens a file, reads its entire contents, and prints it to stdout.

Look at something similar in Java:

   FileReader file = new FileReader("filename.txt") 
   //Then I have to choose how to read the file in, how to
   //buffer it, ect.
   String contents = file.read(...)
   //...
   System.out.print(contents)

In my opinion the Haskell approach is much cleaner and closer to reading a file in Python or Ruby, and more intuitive then trying to figure it out in Java.