How often is seq used in Haskell production code?

Posted by Giorgio on Programmers See other posts from Programmers or by Giorgio
Published on 2012-10-05T21:16:40Z Indexed on 2012/10/05 21:52 UTC
Read the original article Hit count: 235

I have some experience writing small tools in Haskell and I find it very intuitive to use, especially for writing filters (using interact) that process their standard input and pipe it to standard output.

Recently I tried to use one such filter on a file that was about 10 times larger than usual and I got a Stack space overflow error.

After doing some reading (e.g. here and here) I have identified two guidelines to save stack space (experienced Haskellers, please correct me if I write something that is not correct):

  1. Avoid recursive function calls that are not tail-recursive (this is valid for all functional languages that support tail-call optimization).
  2. Introduce seq to force early evaluation of sub-expressions so that expressions do not grow to large before they are reduced (this is specific to Haskell, or at least to languages using lazy evaluation).

After introducing five or six seq calls in my code my tool runs smoothly again (also on the larger data). However, I find the original code was a bit more readable.

Since I am not an experienced Haskell programmer I wanted to ask if introducing seq in this way is a common practice, and how often one will normally see seq in Haskell production code. Or are there any techniques that allow to avoid using seq too often and still use little stack space?

© Programmers or respective owner

Related posts about programming-practices

Related posts about haskell