[Haskell-beginners] case statement

Daniel Trstenjak daniel.trstenjak at gmail.com
Tue Jul 7 11:51:17 UTC 2015


Hi Imants,

On Tue, Jul 07, 2015 at 12:46:25PM +0200, Imants Cekusins wrote:
> is there a way to "freeze" outside variables into constants which can
> be used as "case" statement patterns?

I don't think so.

> result1 x = if x == a1 then "one"
>                    else if x == a2 then "two"
>                    else "three"
>                    where a1 = 1
>                          a2 = 2

There're several other ways to solve this:

   result1 1 = "one"
   result1 2 = "two"
   result1 _ = "three"

Or:

   result1 x 
      | x == 1 = "one"
      | x == 2 = "two"
      | _      = "three"


Greetings,
Daniel


More information about the Beginners mailing list