Reading large file in haskell?

前端 未结 1 1409
一生所求
一生所求 2021-02-02 12:47

i\'ve been trying to read a large file in haskell.

I need to compress it using a custom algorithm for a university project. Everything works fine untill i start to compr

相关标签:
1条回答
  • 2021-02-02 12:51

    The construct "seq x x" is always useless. If y = seq x x and I force y then this forces x then returns x. This is equivalent to y=x and forcing y. Thus "seq forceEval forceEval" does nothing more than "forceEval".

    The error with your use of a fold is a common one.

    You are using a fold to perform a count of the bytes in the input. You should be using a strict left fold for such a sum, but your hand written fold is a lazy left fold. The (acc+1) is not getting evaluated, so it builds 5 million nested applications: (((...(0+1)+1)+1)+1)+1)+1)...+1). Then it is forced when printing, and the evaluation tries to descend into 5 million parentheses.

    Thus the pending stack has one entry for each Word8. For short inputs it reaches the end and sees 0. For long inputs it runs out of stack space with GHC because the creators and most users of GHC think that trying to allocate 5 million stack frames is usually a design error by the programmer.

    I predict that you can use "seq" to fix this:

    fold_tailrec' foldFun acc (x : xs) =
        let acc' = foldFun acc x
        in seq acc' (fold_tailrec' foldFun acc' xs)
    
    0 讨论(0)
提交回复
热议问题