Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  case statement (Imants Cekusins)
   2. Re:  case statement (Imants Cekusins)
   3. Re:  case statement (Daniel Trstenjak)
   4. Re:  case statement (Imants Cekusins)
   5. Re:  case statement (Daniel Trstenjak)


----------------------------------------------------------------------

Message: 1
Date: Tue, 7 Jul 2015 12:21:54 +0200
From: Imants Cekusins <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] case statement
Message-ID:
        <cap1qinb6sbk8hfdo6f9casjpjyz1vmgbhfshwqircprcjfo...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

interestingly, the below test fails. May I ask:

If "case" a1 is not the same as "where" a1, what is its value?
Why does x=1 match against a1?

note: result1 indent is aligned with a1 on the previous line


------  test

module TestCase where
import Test.Hspec

main::IO()
main = hspec $ do
       describe "TestCase" $ do
          it "case 1 - pass" $ do
            result1 1 `shouldBe` "one"
          it "case 2 - fail" $ do
            result1 2 `shouldBe` "two"
       where   a1 = 2
               result1 x = case x of
                              a1 -> "one"
                              2 -> "two"


------------------------------

Message: 2
Date: Tue, 7 Jul 2015 12:28:58 +0200
From: Imants Cekusins <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] case statement
Message-ID:
        <cap1qinyaxmji_g_pt9vcb5vnbphmapy1h2qfhljuau6rrru...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

ok with a small tweak I can see "case" a1 value is x:

result1 x = case x of
               a1 -> trace (show a1) "one"
               2 -> "two"


------------------------------

Message: 3
Date: Tue, 7 Jul 2015 12:36:04 +0200
From: Daniel Trstenjak <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] case statement
Message-ID: <20150707103604.GA13608@brick>
Content-Type: text/plain; charset=us-ascii


Hi Imants,

On Tue, Jul 07, 2015 at 12:28:58PM +0200, Imants Cekusins wrote:
> ok with a small tweak I can see "case" a1 value is x:
> 
> result1 x = case x of
>                a1 -> trace (show a1) "one"
>                2 -> "two"

'a1' is new variable which is always bound to the value of 'x' and this
case is always matching.

You should also get a warning for this:

   Warning:
       Pattern match(es) are overlapped
       In a case alternative: 2 -> ...


Greetings,
Daniel


------------------------------

Message: 4
Date: Tue, 7 Jul 2015 12:46:25 +0200
From: Imants Cekusins <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] case statement
Message-ID:
        <CAP1qinaBV4eTgOtsEeOu+pwxBROsNKiqLbHd4XjacDaN2=a...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

> '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


------------------------------

Message: 5
Date: Tue, 7 Jul 2015 13:51:17 +0200
From: Daniel Trstenjak <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] case statement
Message-ID: <20150707115117.GA14962@brick>
Content-Type: text/plain; charset=us-ascii


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


------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 85, Issue 1
****************************************

Reply via email to