[Haskell-cafe] IO in lists

Magnus Therning magnus at therning.org
Tue Jan 16 06:34:51 EST 2007


Not a very descriptive subject, I know, but here's what I'd like to do.

I can take getChar and create an infinate list:

  listChars = getChar : listChars

but how do I go about creating a finite list, e.g. a list that ends as
soon as 'q' is pressed?

I was thinking of something like

  listChars1 = do
      c <- lift getChar
      if c == 'q'
          then [return c]
          else [return c] ++ listChars1

However, that triggers an interesting behaviour in ghci:

 *Main> :t listChars1
 
 <interactive>:1:0:
     Can't find interface-file declaration for listChars1
       Probable cause: bug in .hi-boot file, or inconsistent .hi file
       Use -ddump-if-trace to get an idea of which file caused the error

Compiling it doesn't work either:

 % ghc -o listio listio.hs
 listio.o: In function `Main_main_info':
 (.text+0x1bd): undefined reference to `Main_listChars1_closure'
 listio.o: In function `Main_main_srt':
 (.rodata+0x10): undefined reference to `Main_listChars1_closure'
 collect2: ld returned 1 exit status

I was also looking briefly at ListT, bout couldn't quite see how to use
basic list operations on it, e.g. ':' '++' etc.

 listChars2 :: ListT IO Char
 listChars2 = do
     c <- lift getChar
     if c == 'q'
     	then lift $ return c
	else (lift $ return c) ++ listChars2

/M

-- 
Magnus Therning                             (OpenPGP: 0xAB4DFBA4)
magnus at therning.org             Jabber: magnus.therning at gmail.com
http://therning.org/magnus

Software is not manufactured, it is something you write and publish.
Keep Europe free from software patents, we do not want censorship
by patent law on written works.

Microsoft has a new version out, Windows XP, which according to everybody
is the 'most reliable Windows ever.' To me, this is like saying that
asparagus is 'the most articulate vegetable ever.'
     -- Dave Barry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20070116/f49531a1/attachment-0001.bin


More information about the Haskell-Cafe mailing list