[Haskell-beginners] Pattern guard inside function
Bob Hutchison
hutch-lists at recursive.ca
Wed Oct 31 18:22:52 CET 2012
Hi,
On 2012-10-31, at 10:02 AM, Nathan Hüsken <nathan.huesken at posteo.de> 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
Cheers,
Bob
>
> Thanks,
> Nathan
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
More information about the Beginners
mailing list