using less stack
Hal Daume III
hdaume@ISI.EDU
Wed, 20 Mar 2002 10:28:13 -0800 (PST)
> strict a = seq a False
>
> foldl' f a l | strict a = annotation
> foldl' f a [] = a
> foldl' f a (x:xs) = foldl' f (f a x) xs
Or, perhaps
strict a = a `deepSeq` False
or
strict a = rnf a `seq` False
if you prefer the rnf notation instead.
depending on what you want...
\begin{rant}
I think the whole seq thing is very confusing for beginning/intermediate
haskell programmers. I was very confused for a long time as to why
> (unsafePerformIO (putStrLn "Hello")) `seq` 5
would print "Hello", but
> [unsafePerformIO (putStrLn "Hello")] `seq` 5
would not. this goes back to the earlier discussion of "does a haskell
programmer need to know how the compiler works." while you could argue
this isn't exactly a compiler issue and that the semantics of seq *are*
well defined outside any particular compiler, you do need to know
something about how the graph reduction happens, etc., in order to
understand exactly what is being evaluated on the left hand side of
`seq`. I would almost prefer if the semantics of `seq` were instead those
of rnf or deepSeq, so long as either (a) we were allowed to derive DeepSeq
or NFData, or (b) if the compiler would just do it itself. Yes, this
would cut down on the expressions which we could `seq`, but does that
really matter. I mean, how often are we going to say:
> (+) `seq` 5
What the heck is that supposed to mean? I would almost *prefer* if an
expression like that didn't typecheck.
Since seq isn't lambda definable anyway, I don't see any particular reason
the compiler couldn't just reduce to normal form instead of weak head
normal form for seqs. Perhaps artifically introduce an NFData instance
for everything so the above doesn't typecheck.
But that's just me :)
\end{rant}