Pattern guard inside function

Hey 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)? Thanks, Nathan

If you want to try some higher order operations on your existing
function, take a look at 'liftM2' and 'join' in Control.Monad. They
won't disappoint!
On Wed, Oct 31, 2012 at 10:02 PM, Nathan Hüsken
Hey
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)?
Thanks, Nathan
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Hi,
On 2012-10-31, at 10:02 AM, Nathan Hüsken
Hey
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)?
maybe like: collInfo'' :: Maybe Rect -> Maybe Rect -> Maybe CollInfo collInfo'' (Just r1@(Rect x1 y1 w1 h1)) (Just r2@(Rect x2 y2 w2 h2)) | x1 + w1 < x2 = Nothing | x1 > x2 + w2 = Nothing | y1 + h1 < y2 = Nothing | y1 > y2 + h2 = Nothing | otherwise = Just $ makeCollInfo r1 r2 collInfo'' _ _ = Nothing Cheers, Bob
Thanks, Nathan
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On 2012-10-31, at 1:22 PM, Bob Hutchison
Hi,
On 2012-10-31, at 10:02 AM, Nathan Hüsken
wrote: Hey
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)?
maybe like:
collInfo'' :: Maybe Rect -> Maybe Rect -> Maybe CollInfo collInfo'' (Just r1@(Rect x1 y1 w1 h1)) (Just r2@(Rect x2 y2 w2 h2)) | x1 + w1 < x2 = Nothing | x1 > x2 + w2 = Nothing | y1 + h1 < y2 = Nothing | y1 > y2 + h2 = Nothing | otherwise = Just $ makeCollInfo r1 r2 collInfo'' _ _ = Nothing
Here's another variation using Maybe as an applicative functor: collInfo''' :: Maybe Rect -> Maybe Rect -> Maybe CollInfo collInfo''' r1 r2 = join $ collInfo <$> r1 <*> r2 The join is in there to get rid a Maybe in (Maybe (Maybe _)). There's probably a way to avoid that but I don't know what it is… I'm learning this stuff myself. Cheers, Bob

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
participants (4)
-
Bob Hutchison
-
Daniel Trstenjak
-
Lyndon Maydwell
-
Nathan Hüsken