3 ms·
> Succinct alternative clause patterns using | This is brilliant. I wish Haskell had this too. > Only case statement, no if I don't quite understand the argu
by runeks 3y ago
> Succinct alternative clause patterns using |
This is brilliant. I wish Haskell had this too.
> Only case statement, no if
I don't quite understand the arguments here. How is that verbose "case" example any better than:
if if_boolean
then "case statements only"
else "you didn't get a choice"
?
- itishappy 3y agoI think the argument is that it's exactly the same, so why learn both? I've definitely lost seconds (maybe even minutes) to refactoring if statements into case statements and vice versa in Haskell.
- Joker_vD 3y agoBecause Erlang's "if" doesn't work like that. It looks like this: if IfBoolean -> "case statements only"; true -> "you didn't get a choice" % yes, it's "true", not "else" end In fact, Erlang's "if" is actually an if-elif chain that takes guards (unlike "case" which takes patterns) as test conditions: those are restricted set of expressions that basically allow only constants, variables, arithmetic/boolean operators and comparisons and very few built-in functions (mostly of the is_type/1 sort), and that's it. Guards are evaluated one by one until one of them evaluates to true; if none do, a run-time error is raised. It's a very unwieldy construct and at my org, we mostly try to avoid it because "case" is clearer in like 98% of the cases.
- lpil 3y agoWhy have two things that do exactly the same?