On Feb 6, 2010, at 00:27 , Andy Elvey wrote:
> However, is my understanding correct that this can be extended to
> lists (arrays in C) so that (for example) for a list ["foo", "bar",
> "baz"] , "pred "bar" " would give you "foo" , and "succ "bar" "
> would give you "baz"?
No. Leaving aside that you don't manipulate lists that way in
Haskell, "bar" is a random value of type String (which is [Char]), not
a member of an enumeration. For comparison:
> data MyType = Foo | Bar | Baz deriving Enum;
> -- pred Bar = Foo, succ Bar = Baz
Some languages (e.g. Perl) do give an enumerable value to Strings, but
`succ "Bar"' would be something like "Baq". (This could be done in
Haskell, with some pain; it starts with `instance (Enum a, Bounded a)
=> Enum [a] where...'.) You can't go from a string like "Bar" to
whatever lists might contain that string (and what if multiple lists
contained it?), so there's no way to get an interpretation like that;
you would need an enumerator which had access both to the list and the
member in question, whereas Enum has access only to the type. (There
exist dependent type systems where you could encode that information
into a defined (sub)type, but Haskell doesn't support it directly.)
What you *can* do is that, because the types of list and array indexes
are members of Enum, you can for example use Data.List.index to
determine the index (if any!) of that item in your list, then take
prev or succ of that. Beware of running off the end of the list,
though. (It's also more complicated for arrays because array indexes
are themselves defined by a typeclass `Ix'.)
--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell]
Code:
Content visible to registered users only.
system administrator [openafs,heimdal,too many hats]
Code:
Content visible to registered users only.
electrical and computer engineering, carnegie mellon university KF8NH
_______________________________________________
Beginners mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)
iEYEARECAAYFAkttBC8ACgkQIn7hlCsL25UwJwCfRzQJ+uZrV7 KA4gPbUkghLbGR
ds8Anjt4VmJmKcso+2eBde4tQLkpTBi0
=lOwV
-----END PGP SIGNATURE-----