looking for System.Console.Readline example

sebc at macs.hw.ac.uk sebc at macs.hw.ac.uk
Tue Dec 2 14:56:32 EST 2003


A minor modification to Martin's code gives you laziness:

> import System.IO.Unsafe
> import System.Console.Readline
> 
> readlines :: String -> IO [String]
> readlines prompt =
>     do input <- readline prompt
>        case input of
>            Nothing ->
>                return []
>            Just str ->
>                do strs <- unsafeInterleaveIO (readlines prompt)
>                   return (str:strs)

*R> readlines ">" >>= mapM (print . length)
>foo
3
>barbaz
6

From the libraries documentation:

unsafeInterleaveIO :: IO a -> IO a 

unsafeInterleaveIO allows IO computation to be deferred lazily. When
passed a value of type IO a, the IO will only be performed when the
value of the a is demanded. This is used to implement lazy file
reading, see hGetContents.

-- 
Sebastien

On Tue, Dec 02, 2003 at 03:34:58PM +0100, Johannes Waldmann wrote:
> Martin Norbäck wrote:
> 
> >What do you mean? Readline is for editing one line. 
> 
> well, yes and no. sure its built-in history functions
> precisely do help editing a sequence of lines?
> as used in bash, ghci, hugs?
> 
> 
> I now have something that works (bottom of this file):
> 
> http://theo1.informatik.uni-leipzig.de/cgi-bin/cvsweb/autotool/Exp/Loop.hs?rev=1.3
> 
> 
> Still I think it would be nice to have
> 
> Readline.getContents :: IO String
> 
> that just returns the lazy input list
> (line by line, as getContents would do).
> 
> It's just another kind of line buffering,
> and this should be transparent to the application.
> (Of course, it couldn't change the prompt symbol then,
> but I could live with that).
> 
> -- 
> -- Johannes Waldmann,  Tel/Fax: (0341) 3076 6479 / 6480 --
> ------ http://www.imn.htwk-leipzig.de/~waldmann/ ---------
> 
> 
> _______________________________________________
> Haskell mailing list
> Haskell at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://haskell.org/pipermail/haskell/attachments/20031202/9fc6d5f1/attachment.bin


More information about the Haskell mailing list