[Haskell-beginners] data type design question

Markus Barenhoff alios at alios.org
Wed Jul 30 08:43:31 EDT 2008


Hi Tilmann,

On Wed, Jul 30, 2008 at 02:10:07PM +0200, Tillmann Rendel wrote:
> Hi Markus,
> 
> Markus Barenhoff wrote:
> >I have written a parsec parser for reading a marshallaled dataformat.
> >
> >The returned data structure is based on the following data type:
> >
> >>data T = TString String
> >>       | TInt Integer
> >>       | TList [TorrentT]
> >>       | TDict [(TorrentT, TorrentT)]
> >
> >I think TString and TInt are clear. The elements of a TList always have
> >the same "type" (same constructor). The TDict is a dictionary where the 
> >key is always a TString but the value can be of any of the other "types",
> >even in the same dictionary. F.e the key "foo" may map to a TInt while the
> >key "bar" maps to another TDict.
> 
> Maybe something like
> 
>   data T = TString String
>          | TInt Integer
>          | TStringList [String]
>          | TIntList [Integer]
>          | TDict [(String, T)]

mmm lists could also contain list or dict, so on must introduce constructors
for those too. You will run in problems there because what is with a list of
lists then you would need a TIntListList and so on... 

Maybe some words to the background why I use one type. This is to make the 
parsec parser work.
The toplevel parser looks like this, because the parsed text could start with
any of them:

> parser :: GenParser Char st T
> parser =
>     do stringParser <|> integerParser <|> listParser <|> dictParser


Markus

-- 
Markus Barenhoff - Münster - Germany - Europe - Earth
e-mail: alios at alios.org - jabber: alios at jabber.ccc.de - icq: 27998346

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 479 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/beginners/attachments/20080730/f45af6e2/attachment.bin


More information about the Beginners mailing list