|Version 4 (modified by malcolm.wallace@…, 9 years ago) (diff)|
Very often I find myself wanting to write an if-then-else with multiple branches. This is one expression form that isn't catered for well at the moment. Using nested 'if' is unsatisfactory because the nesting grows with each branch, and all those keywords are quite syntactically heavy.
One idiom that is often used for this is:
case () of _ | pred1 -> e1 ... | predn -> en
this is very powerful in conjunction with PatternGuards, incidentally. But it's ugly. I propose a simpler form:
case | pred1 -> e1 ... | predn -> en
There are no syntactic problems with this because it is uniquely identified by the sequence 'case |' which is currently not a valid expression prefix in Haskell. It's therefore backwards compatible.
Semantics can be defined as shorthand for the original case statement above.
Cons: it isn't implemented anywhere.
Currently, guards are permitted at the definition clause level. But this proposal can be viewed as permitting guards at the expression level too. In the spirit of reducing unnecessary duplication, it would then be possible to remove definition-level guards from the language. The current form
foo (x:xs) | x < n = ... | x > n = ...
can be straightforwardly replaced by
foo (x:xs) = case | x < n -> ... | x > n -> ...
Note the change in the equals sign, from one per guard to one per clause. Case-style alternatives use -> instead of =. However, this removal would break old programs.