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 = 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"
where a1 = 1
a2 = 2
Beginners mailing list
[hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
On Tue, Jul 7, 2015 at 8:04 AM, Imants Cekusins <[hidden email]> wrote:
This "case" behaviour (patterns allow no access to outer scope
variables) is different from some other languages.
In most functional languages, "case" is about structure, not values. You specify the structure in terms of constructors, which must be literal. Variables in patterns always capture values associated with those constructors, never to values outside the case expression.
That said, doing a value-based comparison ("switch"-type construct) is rather annoying, and most of Haskell's (and ghc extension) mechanisms only reduce the pain somewhat. :/