[Haskell-cafe] Why is this strict in its arguments?
ndmitchell at gmail.com
Tue Dec 4 17:00:25 EST 2007
> findAllPath :: (a -> Bool) -> (BTree a) -> [[a]]
> findAllPath pred = g where
> g (Leaf l) | pred l = [[l]]
> g (Branch lf r rt) | pred r = map (r:) $ (findAllPath pred
> lf) ++ (findAllPath pred rt)
> g _ = 
> without even using maybe. However, 2 questions remained:
> 1 - why is the first version strict in its arguments?
Because in all call paths findAllPath will call g with its second
argument. g will always evaluate (by pattern matching on) its value
> 2 - if it really is strict in its arguments, is there any automated
> way to know when a function is strict in its arguments?
Yes, strictness analysis is a very well studied subject -
. Essentially, an argument is strict if passing _|_ for that value
results in _|_. So to take your example, evaluating:
findAllPath a _|_
Since g tests what value _|_ has, we get bottom.
More information about the Haskell-Cafe