[Haskell-cafe] Why is this strict in its arguments?

Paulo J. Matos pocm at soton.ac.uk
Tue Dec 4 16:41:36 EST 2007

Hello all,

As you might have possibly read in some previous blog posts:

we (the FPSIG group) defined:
data BTree a = Leaf a
                   | Branch (BTree a) a (BTree a)

and a function that returns a list of all the paths (which are lists
of node values) where each path element makes the predicate true.
findAllPath :: (a -> Bool) -> (BTree a) -> Maybe [[a]]
findAllPath pred (Leaf l) | pred l = Just [[l]]
                          | otherwise = Nothing
findAllPath pred (Branch lf r rt) | pred r = let lfpaths = findAllPath pred lf
                                                 rtpaths = findAllPath pred rt
                                               if isNothing lfpaths &&
isNothing rtpaths
                                               then Nothing
                                                   if isNothing lfpaths
                                                   then Just (map (r:)
$ fromJust rtpaths)
                                                       if isNothing rtpaths
                                                       then Just (map
(r:) $ fromJust lfpaths)
                                                       else Just (map
(r:) $ fromJust rtpaths ++ fromJust lfpaths)
                                  | otherwise = Nothing

Later on we noticed that this could be simply written as:
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?
2 - if it really is strict in its arguments, is there any automated
way to know when a function is strict in its arguments?


Paulo Jorge Matos - pocm at soton.ac.uk
PhD Student @ ECS
University of Southampton, UK

More information about the Haskell-Cafe mailing list