Re: Case expressions, matching, and constants

2003-07-18 Thread Remi Turk
On Thu, Jul 17, 2003 at 12:03:19PM +0100, Bayley, Alistair wrote:
 This is what I've turned it into to get it to work. It seems a bit clumsy;
 is there a better way to write this?
 
  test n =
  case True of
  _ | n == one - one
| n == two - two
| otherwise - three

Or you might want to use something like this:
(depending on how much your example resembles the actual code
and the number of variables you want to match)

module Main where

one = 1
two = 2

test n  = maybe d id (n `lookup` m)
where
m   = [(one,one)
  ,(two,two)
  ]
d   = three
-- 
Nobody can be exactly like me. Even I have trouble doing it.


pgp0.pgp
Description: PGP signature


Re: Case expressions, matching, and constants

2003-07-17 Thread Graham Klyne
At 12:03 17/07/03 +0100, Bayley, Alistair wrote:
This is what I've turned it into to get it to work. It seems a bit clumsy;
is there a better way to write this?
 test n =
   case True of
   _ | n == one - one
 | n == two - two
 | otherwise - three
The 'case' is somewhat redundant.  How about:

 test n | n == one  = one
| n == two  = two
| otherwise = three
#g

---
Graham Klyne
[EMAIL PROTECTED]
PGP: 0FAA 69FF C083 000B A2E9  A131 01B9 1C7A DBCA CB5E
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: Case expressions, matching, and constants

2003-07-17 Thread Hamilton Richards
At 12:03 PM +0100 7/17/03, Bayley, Alistair wrote:
I've just debugged a program that used a case expression, but where I was
trying to match on constants rather than literals. Here's a contrived
example:

 module Main where
 one = 1
 two = 2
 test n =
case n of
one - one
two - two
_ - three
 main = putStrLn (test 2)


This initial version seems to me to be the natural way to write the case
expression, but it doesn't work because the first alternative always
succeeds.
The root of the problem is that a variable occurring in a pattern is 
always a new variable. A pattern variable provides a way to refer to 
the value to which the variable is bound when the pattern matches.

This is what I've turned it into to get it to work. It seems a bit clumsy;
is there a better way to write this?
 test n =
case True of
_ | n == one - one
  | n == two - two
 		  | otherwise - three
As, Graham Klyne wrote at 2:52 PM +0100 7/17/03:

 test n | n == one  = one
| n == two  = two
| otherwise = three
is a neater solution.

Best,

--Ham
--
--
Hamilton Richards, PhD   Department of Computer Sciences
Senior Lecturer  The University of Texas at Austin
512-471-9525 1 University Station C0500
Taylor Hall 5.138Austin, Texas 78712-1188
[EMAIL PROTECTED][EMAIL PROTECTED]
--
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


RE: Case expressions, matching, and constants

2003-07-17 Thread Mark P Jones
Hi Alistair,

| I've just debugged a program that used a case expression, but 
| where I was trying to match on constants rather than literals.
| Here's a contrived example:
| 
|  module Main where
|  one = 1
|  two = 2
| 
|  test n =
|  case n of
|  one - one
|  two - two
|  _ - three
| 
|  main = putStrLn (test 2)

There's a 1992 tech report by Aitken and Reppy that provides a proposal
for doing this kind of thing (and a bit more) in the context of SML:

 ftp://ftp.research.bell-labs.com/dist/smlnj/papers/92-tr-aitken.ps

All the best,
Mark

___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe