1
\$\begingroup\$

The new MultiWayIf extension (available with GHC 7.6) allows guard syntax in an if:

{-# LANGUAGE MultiWayIf #-}
fn :: Int -> String
fn x y = if | x == 1    -> "a"
            | y <  2    -> "b"
            | otherwise -> "c"

But I don't find it better than the old way:

fn :: Int -> String
fn x y | x == 1    = "a"
       | y <  2    = "b"
       | otherwise = "c"

Has it sense in other cases?

\$\endgroup\$

closed as off-topic by Jamal Apr 26 '18 at 23:11

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ I am not sure if this question is suited for Code Review, maybe it should get migrated to SO. \$\endgroup\$ – Flow Apr 8 '13 at 10:00
  • \$\begingroup\$ Flow, I put in codereview because is probably more subjective that a question in SO. It, IMHO, is a "Best practices and design pattern usage" question \$\endgroup\$ – Zhen Apr 8 '13 at 10:06
  • \$\begingroup\$ Good point, seems reasonable. \$\endgroup\$ – Flow Apr 8 '13 at 10:09
8
\$\begingroup\$

Unsurprisingly, it's mainly useful when the if is not the top-level expression. Say:

forM_ [1..100] $ \i ->
  putStrLn $ if | i `mod` 15 == 0  -> "FizzBuzz"
                | i `mod` 3 == 0   -> "Fizz"
                | i `mod` 5 == 0   -> "Buzz"
                | otherwise        -> show i

I happen across similar cases quite often - up to this point this required either deep if trees or mis-using case in some way.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.