[Haskell-beginners] A first try

Daniel Fischer daniel.is.fischer at googlemail.com
Mon Jun 27 11:49:37 CEST 2011


On Monday 27 June 2011, 11:15:13, Manfred Lotz wrote:
> 
> I tried now:
> 
> getXmlContent :: String -> Handle -> IO Ctan
> getXmlContent xf inh = do
>     xml <- U.hGetContents inh
>     let content = xml `deepseq` parseXMLDoc xml
>     case content of
>     ...
> 
> deepseq really ensures parseXmlDoc gets the full file as a string.
> 
> It is unclear to me how this could be done without deepseq.

To ensure that the entire file is read, you can seq on the length,

    xml <- U.hGetContents inh
    let content = parseXMLDoc xml
    length xml `seq` case content of ...

that might be faster than deepseq (or it might make no difference, deepseq 
is cheap on Chars).
In general, it is often good to avoid deepseq if that would traverse large 
data structures which are already evaluated (consider modifying a Map or 
something similar, if you force it with deepseq, each alteration is 
O(size), so it's worth some effort to find a way to only force the modified 
parts to have the alterations be O(log size)).




More information about the Beginners mailing list