Case expressions, matching, and "constants"
Remi Turk
rturk@science.uva.nl
Fri, 18 Jul 2003 22:10:57 +0200
--0OAP2g/MAC+5xKAE
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
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?
>=20
> > test n =3D
> > case True of
> > _ | n =3D=3D one -> "one"
> > | n =3D=3D 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 =3D 1
two =3D 2
test n =3D maybe d id (n `lookup` m)
where
m =3D [(one, "one")
,(two, "two")
]
d =3D "three"
--=20
Nobody can be exactly like me. Even I have trouble doing it.
--0OAP2g/MAC+5xKAE
Content-Type: application/pgp-signature
Content-Disposition: inline
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
iD8DBQE/GFRR36VhKIVjE3sRAhviAKCzzHTwHh/qxL8D1Sp950BaOsh+twCgoVJ3
WXX+JvFrTSpCAKNds5zgwrs=
=irHr
-----END PGP SIGNATURE-----
--0OAP2g/MAC+5xKAE--