[Haskell-beginners] A first try

Antoine Latter aslatter at gmail.com
Mon Jun 27 05:39:42 CEST 2011


On Mon, Jun 27, 2011 at 11:57 AM, Manfred Lotz <manfred.lotz at arcor.de> wrote:
> On Sun, 26 Jun 2011 17:32:02 -0400
> David Place <d at vidplace.com> wrote:
>
>>
>>
>> On Jun 26, 2011, at 4:59 PM, Manfred Lotz wrote:
>>
>> > Is there any problem in the code snippet I pasted? If so I would
>> > like to get a hint of course.
>>
>> There really isn't enough code in your snippet to be able to say.
>> The rule is that if you return a lazy data structure from withFile
>> you may find the file has been closed before you have read the data.
>> If your parseXMLDoc is strict then you will get away with it.   I
>> feel that isn't very nice.
>>
>
> What I had learned (and it seems to work fine) is to force the result
> which gets done here in return $!
>
> insertXml (stat, m) xf =
>  U.withBinaryFile xf ReadMode
>     (\handle -> do ct <- getXmlContent xf handle
>          ... some code...
>         return $! (stat',m'))
>
> So I hope that is the one and important recipe to add when using
> withBinaryFile.
>

The operator ($!) isn't recursive, so all that it does is force the
tuple you're returning, not the contents of the tuple.

Internally it calls 'seq', which only forces its argument enough to
determine which constructor it was called with. Sometimes this can be
enough to do what you want, but not usually. And certainly not in this
case.

Antoine

>
>
> --
> Manfred
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



More information about the Beginners mailing list