
On Mon, 18 Aug 2003 16:21:46 +0100
Keith Wansbrough
Hello, When I have started my project, I use a Tuples but i would know if it is possible to create a record such C or Ocaml provide. I mean creating a structure where variables are accessible by a '.' or something like that.
Yes. Like this:
data Tree a = Node { key :: Int, val :: a, left, right :: Tree a } | Nil deriving Show
hmm, maybe I shouldn't say this, but record syntax does not preclude positional syntax. You can still pattern match and construct Node as if it was defined Node Int a (Tree a) (Tree a), i.e. Node 1 "foo" Nil Nil or f (Node 0 _ _ _) = ... Of course, this brings back all the issues of positional notation, so you may not want to use this... um, ever. However, one issue with records is that it's possible (or rather easier) to make partially defined records. E.g. Node { key = 5 } is legal, and any attempt to use val/left/right will cause a run-time error. So this might be a use for the alternate positional syntax. Code that uses the positional syntax to build records will break when fields are added to the record rather than go on creating broken records (of course, GHC at least, produces a warning for uninitialized fields).