Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

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


Today's Topics:

   1.  Using symbolic values in the case expression (C K Kashyap)
   2. Re:  Using symbolic values in the case expression (Thomas Davie)
   3. Re:  Using symbolic values in the case expression (Daniel Fischer)
   4. Re:  Using symbolic values in the case expression (C K Kashyap)
   5. Re:  Using symbolic values in the case expression (Ozgur Akgun)
   6. Re:  Using symbolic values in the case expression (Daniel Fischer)


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

Message: 1
Date: Thu, 11 Nov 2010 22:33:10 +0530
From: C K Kashyap <ckkash...@gmail.com>
Subject: [Haskell-beginners] Using symbolic values in the case
        expression
To: beginners@haskell.org
Message-ID:
        <aanlktime3nmfbhocozgxbr5p22xys9hvbsu_hndsc...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

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?


-- 
Regards,
Kashyap


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

Message: 2
Date: Thu, 11 Nov 2010 17:06:06 +0000
From: Thomas Davie <tom.da...@gmail.com>
Subject: Re: [Haskell-beginners] Using symbolic values in the case
        expression
To: C K Kashyap <ckkash...@gmail.com>
Cc: beginners@haskell.org
Message-ID: <bedde69f-d171-44ee-9464-0c97b356a...@gmail.com>
Content-Type: text/plain; charset=us-ascii


On 11 Nov 2010, at 17:03, 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?

Pattern matching against expressions is not possible, except in the very very 
limited case of n + k patterns, so no.

Bob

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

Message: 3
Date: Thu, 11 Nov 2010 18:24:34 +0100
From: Daniel Fischer <daniel.is.fisc...@web.de>
Subject: Re: [Haskell-beginners] Using symbolic values in the case
        expression
To: beginners@haskell.org
Message-ID: <201011111824.34671.daniel.is.fisc...@web.de>
Content-Type: text/plain;  charset="iso-8859-1"

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.


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

Message: 4
Date: Thu, 11 Nov 2010 23:15:43 +0530
From: C K Kashyap <ckkash...@gmail.com>
Subject: Re: [Haskell-beginners] Using symbolic values in the case
        expression
To: Daniel Fischer <daniel.is.fisc...@web.de>
Cc: beginners@haskell.org
Message-ID:
        <aanlktinqebsabqxzqnfi7armqz2sugvvww=gapqr9...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Is there a way out?
What I need to do is implement a protocol - so it would be nice to
write a case expression with patters being symbolic.
-- 
Regards,
Kashyap


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

Message: 5
Date: Thu, 11 Nov 2010 18:42:32 +0000
From: Ozgur Akgun <ozgurak...@gmail.com>
Subject: Re: [Haskell-beginners] Using symbolic values in the case
        expression
To: C K Kashyap <ckkash...@gmail.com>
Cc: beginners@haskell.org
Message-ID:
        <aanlktinjrptq5z_rsbjdsvjvkc5xu6hczkdg93drh...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On 11 November 2010 17:03, C K Kashyap <ckkash...@gmail.com> wrote:

> v1 = 1
> v2 = 2
>
> f x = case x of
>  v1 -> "One"
>  v2 -> "Two"
>

The closest I can thin is the following:

f x | x == v1 = "One"
    | x == v2 = "Two"

But, keep in mind, this is *not* the same thing. It requires Eq on x, and
you lose all the exhaustiveness/overlapping checks pattern matching
provides.

HTH,

-- 
Ozgur Akgun
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20101111/76cfcf5b/attachment-0001.html

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

Message: 6
Date: Thu, 11 Nov 2010 19:18:33 +0100
From: Daniel Fischer <daniel.is.fisc...@web.de>
Subject: Re: [Haskell-beginners] Using symbolic values in the case
        expression
To: C K Kashyap <ckkash...@gmail.com>
Cc: beginners@haskell.org
Message-ID: <201011111918.33536.daniel.is.fisc...@web.de>
Content-Type: text/plain;  charset="iso-8859-1"

On Thursday 11 November 2010 18:45:43, C K Kashyap wrote:
> Is there a way out?
> What I need to do is implement a protocol - so it would be nice to
> write a case expression with patters being symbolic.

Guards?

case expr of
  res | res == v1 -> "One"
      | res == v2 -> "Two"
  _ -> error "Three"

Or perhaps a little preprocessor abuse might help.

{-# LANGUAGE CPP #-}

#define V1 1
#define V2 2

case expr of
  V1 -> "One"
  V2 -> "Two"
  _  -> error "Three"


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

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 29, Issue 17
*****************************************

Reply via email to