[Haskell-beginners] data constructors

Michael Mossey mpm at alumni.caltech.edu
Sun Apr 19 17:30:28 EDT 2009

This is probably a side-effect of coming from OO land, but I'm confused about how to 
organize my data. Let's say I want to model musical information that occurs on a 
single staff. A staff consists of a list of StaffItem which occurs at specific times.

type Time = Double
type Staff = [(Time,StaffItem)]

A StaffItem can be one of several things. Let's say it can be a "chord" or a 
"control". I might like to define Chord and Control first:

data Chord = Chord { duration :: Double, notes :: [Note] }
data Control = DynamicMark Int
              | TempoMark Int

Okay, so now I want to express the idea a StaffItem can be a Chord or a Control.

data StaffItem = StaffItemChord Chord
                | StaffItemControl Control

My problem is the awkward need for separately named constructors  "StaffItemChord" 
and "StaffItemControl". Is there a better way? (Is this even right?)


More information about the Beginners mailing list