More suitable data structure needed

Dylan Thurston dpt@math.harvard.edu
Wed, 21 Aug 2002 04:20:39 -0400


--dc+cDN39EJAMEtIO
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Wed, Aug 21, 2002 at 04:44:03PM +0930, Dr Mark H Phillips wrote:
> ...
> I would like to represent this structure in Haskell, but=20
> am not sure quite the best way of doing it.  (I am relatively
> new to Haskell.)  I think I want to do something like:
>=20
> [
> [(2,5),[(1,3),[(2,0)]],
>        [(1,2),[(1,1),[(1,0)]]],
>        [(3,1)]],
> [(1,5),[(2,4),[(2,0)]],
>        [(1,4),[(1,3),[(1,1),[(1,0)]]],
>               [(2,2),[(1,0)]],
>               [(1,2),[(2,1)]]],
>        [(2,3),[(1,2),[(1,0)]],
>               [(2,1)]],
>        [(1,3),[(2,2),[(1,1)]]],
>        [(4,2)]]
> ]
>=20
> But what is the best way to represent this in Haskell?  (Clearly
> I can't do exactly this, because Haskell requires all list elements
> to be of the same type.)

This is the same as one way of representing search trees, called a
"trie".  Two representations in Haskell are:

> data Trie a =3D Trie [(a, Trie a)]

or, using the FiniteMap module, if you only care about the set of
lists,

> data Trie a =3D Trie (FiniteMap a (Trie a))

(There are slight variations depending on your exact needs.)

Best,
	Dylan

--dc+cDN39EJAMEtIO
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE9Y01XVeybfhaa3tcRAssPAJ9ovVbmrL6Lh40Vv9SWKepSYdsdUQCfbwB4
uaN3VMfPwPPg5sI3Wq4RptM=
=pAUE
-----END PGP SIGNATURE-----

--dc+cDN39EJAMEtIO--