LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Blogs > Bits and Pixels
User Name
Password

Notices

Concerning free software, programming, and whatever else I find interesting. Also the blog for my Web site, frigidcode.com.
Rate this Entry

haskell: get lines and handle end of file

Posted 01-01-2013 at 02:23 AM by hydraMax
Updated 01-01-2013 at 02:27 AM by hydraMax
Tags haskell

I thought I'd post this convenience function, since I didn't see an obvious one in base:

Code:
import Prelude hiding (catch)
import Control.Exception
import System.IO.Error hiding (catch)

readl f = catch (getLine >>= return . Just)
                (\e -> if (isEOFErrorType . ioeGetErrorType) e
                         then return Nothing
                         else ioError e)
          >>= \a -> case a of
                     Nothing -> return ()
                     Just b -> f b >> readl f
Pass in a function which takes a string and does IO. EOF (end of file) causes the loop to stop. Other IO errors are simply raised again. For example:

Code:
main = readl (\x -> putStrLn (x ++ " bleep"))

$ ./test.hs 
hi 
hi bleep
what?
what? bleep
Permission is given to reuse this code without restriction.
Posted in Uncategorized
Views 662 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 03:16 AM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration