[Haskell-beginners] Pattern guard inside function

Lyndon Maydwell maydwell at gmail.com
Wed Oct 31 15:16:18 CET 2012


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
<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)?
>
> Thanks,
> Nathan
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners



More information about the Beginners mailing list