<html><body>Use a tuple: (Result,Maybe Error) rather than an Either.&nbsp; Do everything lazily, and in the case of an error, undo the result.<br><p><br>---------- Původní zpráva ----------<br>Od: Eric Velten de Melo &lt;ericvmelo@gmail.com&gt;<br>Datum: 11. 9. 2012<br>Předmět: [Haskell-cafe] Either Monad and Laziness</p><blockquote>Hello,<br><br>I am currently trying to rewrite the Graphics.Pgm library from hackage<br>to parse the PGM to a lazy array. The current implementation parses it<br>straight to UArray, which is strict.<br><br>The behaviour I want to achieve is like this: I want the program when<br>compiled to read from a file, parsing the PGM and at the same time<br>apply transformations to the entries as they are read and write them<br>back to another PGM file.<br><br>The problem is that even using a lazy array structure, because the<br>parser returns an Either structure it is only possible to know if the<br>parser was successful or not after the whole file is read, therefore<br>requiring it to read the entire file before applying the<br>transformations, ruining the property of laziness.<br><br>Is there some way to keep this from happening? Should I even want to<br>make it like this? Not really a real life situation, but imagine I<br>want to read a reaaaaally large PGM file which does not fit into RAM<br>memory and I don't want to be forced to have the whole array in the<br>memory at once.<br><br>One alternative I thought was parsing only the PGM header and then<br>read the rest of the input without using Parsec and the Either Monad.<br>In the event the data is corrupted, though, I would not know how to<br>recover from it.<br><br>Any thoughts? Hopefully I'm not saying anything really stupid.<br><br>Eric<br><br>_______________________________________________<br>Haskell-Cafe mailing list<br>Haskell-Cafe@haskell.org<br><a href="http://www.haskell.org/mailman/listinfo/haskell-cafe">http://www.haskell.org/mailman/listinfo/haskell-cafe</a></blockquote></body></html>