[Haskell-beginners] case statement

Imants Cekusins imantc at gmail.com
Tue Jul 7 10:46:25 UTC 2015


> 'a1' is new variable

Thank you Daniel!

is there a way to "freeze" outside variables into constants which can
be used as "case" statement patterns?

for example if I use "if", the below test passes.

It would be handy to use case statement instead of multiple ifs or guards

main::IO()
main = hspec $ do
       describe "TestCase" $ do
          it "case 1 - pass" $ do
            result1 1 `shouldBe` "one"
          it "case 2 - pass" $ do
            result1 2 `shouldBe` "two"
       where   result1 x = if x == a1 then "one"
                           else if x == a2 then "two"
                           else "three"
                           where a1 = 1
                                 a2 = 2


More information about the Beginners mailing list