[Haskell-beginners] Using symbolic values in the case expression

Daniel Fischer daniel.is.fischer at web.de
Thu Nov 11 12:24:34 EST 2010


On Thursday 11 November 2010 18:03:10, C K Kashyap wrote:
> Hi,
> I'd like to do something like this -
>
> Instead of doing this -
>
> f x = case x of
>    1 -> "One"
>    2 -> "Two"
>
> I'd like to do this -
>
> v1 = 1
> v2 = 2
>
> f x = case of
>   v1 -> "One"
>   v2 -> "Two"
>
>
> Is that possible?

No, case does a pattern match, it checks whether the expression matches the 
pattern (patterns are defined in the report, section 3.17, 
http://www.haskell.org/onlinereport/haskell2010/haskellch3.html#x8-580003.17), 
so variable names (identifiers beginning with a lower case letter) match 
everything.


More information about the Beginners mailing list