[Haskell-cafe] Question on "case x of g" when g is a function

Bayley, Alistair Alistair_Bayley at ldn.invesco.com
Thu Jan 27 07:21:15 EST 2005


Another way of looking at it: case peforms pattern matching; it is _not_ an
equality test.

If you want equality tests, use if-then-else, or something like this (using
guards):

f x = case () of
  _ | x == bit0 -> 0
    | x == bit1 -> 1

The behaviour that initially threw me was that case works for various
literals (numbers and strings), but that's just pattern matching (see
3.17.2, item 7):
  http://www.haskell.org/onlinereport/exps.html#pattern-matching

Another shorthand for f, using guards:

f x
  | x == bit0 = 0
  | x == bit1 = 1


> -----Original Message-----
> From: Salvador Lucas [mailto:slucas at dsic.upv.es] 
> Sent: 27 January 2005 09:59
> To: yeoh at cs.wisc.edu
> Cc: haskell-cafe at haskell.org
> Subject: Re: [Haskell-cafe] Question on "case x of g" when g 
> is a function
> 
> Because both bit0 and bit1 are free *local* variables
> within the case expression. So, they have nothing
> to do with your defined functions bit0 and bit1.
> 
> Best regards,
> 
> Salvador.
> 
> yeoh at cs.wisc.edu wrote:
> 
> >Can a kind soul please enlighten me on why f bit0 and f bit1 
> >both return 0?
> >
> >  
> >
> >>bit0     = False
> >>bit1     = True
> >>f x = case x of
> >>        bit0 -> 0
> >>        bit1 -> 1
> >>    

-----------------------------------------
*****************************************************************
Confidentiality Note: The information contained in this   message, and any
attachments, may contain confidential   and/or privileged material. It is
intended solely for the   person(s) or entity to which it is addressed. Any
review,   retransmission, dissemination, or taking of any action in
reliance upon this information by persons or entities other   than the
intended recipient(s) is prohibited. If you received  this in error, please
contact the sender and delete the   material from any computer.
*****************************************************************



More information about the Haskell-Cafe mailing list