More suitable data structure needed

Hal Daume III hdaume@ISI.EDU
Thu, 22 Aug 2002 07:58:32 -0700 (PDT)


Hi again,

>   data Age = Age Int
>   data Names = Names [String]
>   data Person = Person (Age,Names)
> 
> can be used to represent the details of a person, but the "essential
> type" corresponding to Person, is
> 
>   (Int,[String])

In Haskell, at least, this is not entirely true.  When you say

  data Age = Age Int

you are introducing an isomorphic type, but not an identical type.  The
differences can be seen due to type classes.  You could define, for
instance, a different instance of Eq on Age than exists on Int.  This is
another major use of defining these simple type isomorphisms (note you
could also say 'newtype Age = Age Int').

> > > data Trie a = Trie (FiniteMap a (Trie a))
> 
> Am I right in thinking that FiniteMap is like a list, but that it is
> more efficient with "random access" use than a normal list?

Yes, certainly a FiniteMap will be faster (O(lg N) vs O(N)) for large
structures.  But it also means that the type 'a' must be an instance of
Ord (for you, this doesn't seem to matter).

 - Hal

p.s., I leave the elided questions for someone else.