[Haskell-beginners] Pattern guard inside function

Nathan Hüsken nathan.huesken at posteo.de
Wed Oct 31 15:02:45 CET 2012


I have a function which originaly locked like this:

collInfo :: Rect -> Rect -> Maybe CollInfo
collInfo (Rect x1 y1 w1 h1) (Rect x2 y2 w2 h2)
  | x1 + w1 < x2 = Nothing
  | x1 > x2 + w2 = Nothing
  | y1 + h1 < y2 = Nothing
  | y1 > y2 + h2 = Nothing
  | otherwiese = Just $ makeCollInfo r1 r2

Now I want to refactor it to take Maybe input values

collInfo' :: Maybe Rect -> Maybe Rect -> Maybe CollInfo
collInfo' mr1 mr2 =
  r1 <- mr1
  r2 <- mr2
  collInfo r1 r2

This is fine, but I would like to write it using only one function. Is
there any possibility I can remove collInfo and merge its body into
collInfo' and still somehow use the guards (and not a bunch of ifs)?


More information about the Beginners mailing list