Strictness!
Carl R. Witty
cwitty@newtonlabs.com
18 Mar 2002 16:07:41 -0800
Jay Cox <sqrtofone@yahoo.com> writes:
> On Thu, 14 Mar 2002, Brian Huffman wrote:
>
> > In Haskell you can produce the desired behavior by using pattern guards.
> > Since the pattern guards always get evaluated before the result does, they
> > can be used to make things more strict. Here is the foldl example:
> >
> > strict x = seq x True
> >
> > foldl' :: (a -> b -> a) -> a -> [b] -> a
> > foldl' f z [] = z
> > foldl' f z (x:xs)
> > | strict z = foldl' f (f z x) xs
> >
> > You can make multiple arguments strict by using && or similar, e.g.:
> >
> > f x y | strict x && strict y = ...
> > f x y | all strict [x,y] = ...
> >
> > Of course, with the second example x and y must have the same type.
> > Hope this helps.
> >
> > - Brian Huffman
>
> Thanks! that looks like a great haskell idiom to use!
>
> Jay Cox
I also like this for trace:
f x y
| trace (show (x, y)) True
= ...
(Putting the trace on a separate line like that makes it easy to
comment out.)
Or, if you've got a function with a lot of cases, you can trace all of
them by adding:
f x y | trace (show (x, y)) False = undefined
above all the other cases.
Carl Witty