[Haskell-beginners] Pattern guard inside function
Daniel Trstenjak
daniel.trstenjak at gmail.com
Thu Nov 1 12:06:18 CET 2012
Hi Nathan,
On Wed, Oct 31, 2012 at 03:02:45PM +0100, Nathan Hüsken wrote:
> collInfo' :: Maybe Rect -> Maybe Rect -> Maybe CollInfo
> collInfo' mr1 mr2 =
> r1 <- mr1
> r2 <- mr2
> collInfo r1 r2
that doesn't seem to be that nice, because you could call it with
any two Rects and get a CollInfo, even if they don't collide:
collInfo' (Just $ Rect 0 0 1 1) (Just $ Rect 5 5 1 1)
Having Maybes as arguments just doesn't feel that right.
You could separate the collision test:
colliding :: Rect -> Rect -> Bool
colliding (Rect x1 y1 w1 h1) (Rect x2 y2 w2 h2) =
not $ x1 + w1 < x2
|| x1 > x2 + w2
|| y1 + h1 < y2
|| y1 > y2 + h2
collInfo :: Rect -> Rect -> Maybe (Rect, Rect)
collInfo r1 r2
| colliding r1 r2 = Just (r1, r2)
| otherwise = Nothing
Greetings,
Daniel
More information about the Beginners
mailing list