Thanks for all the advice. In the end, I couldn't make $! work for me (it always seems to be harder than I think it will be to use it, and $! and deepSeq makes my code run slowly).
:-(
But a continuation passing style foldl worked wonderfully.
As Jay Cox pointed out by email, my answer was rot because I had confused foldl and foldr
I now have:
cpsfold f a [] = a cpsfold f a (x:xs) = f x a (\y -> cpsfold f y xs)
and f takes a continuation, Bob's my uncle, and I have a program that runs quickly in constant space!
Good. I'm curious to know from other readers whether continuations like this are the only way of solving it, though. Jón -- Jón Fairbairn Jon.Fairbairn@cl.cam.ac.uk 31 Chalmers Road jf@cl.cam.ac.uk Cambridge CB1 3SZ +44 1223 570179 (after 14:00 only, please!)