Common syntax for casing/matching (Re[2]: Call to arms: lambda-case is stuck and needs your help)
Twan van Laarhoven
twanvl at gmail.com
Fri Jul 13 00:58:42 CEST 2012
On 2012-07-12 23:48, Bulat Ziganshin wrote:
> another interesting feature may be ruby-style matching defined by
> execution of special function `match` instead of pattern matching:
>
> switch var of
> 1+1 -> print "var==2"
> [5..10] -> print "var in [5..10]"
> (>20) -> print "var>20"
>
> where (var `match` (1+1)), (var `match` [5..10]), (var `match` (>20)) is tested
With view patterns you can write
case var of
((== 1+1) -> True) -> print "var==2"
((`elem` [5..10]) -> True) -> print "var in [5..10]"
((> 20) -> True) -> print "var>20"
Or you can just use guards, of course.
Twan
More information about the Glasgow-haskell-users
mailing list