[Haskell-beginners] Can fields in a record be optional?

Costello, Roger L. costello at mitre.org
Mon Jul 18 00:03:08 CEST 2011


Hi Folks,

Can fields in a record be made "optional"? 

Let me motivate my question:

I have a data type called "Contract". It is a record. It has one field that holds a currency value, a second field that holds payments, and a third field to hold sub-Contracts.

data Contract =  Contract {
                     currency  :: Currency
                   , payments  :: Double
                   , contracts :: [Contract]
                 }
                 deriving (Show)

Here is a function that creates a Contract with currency c and payments = 1:

one    :: Currency -> Contract
one c  =  Contract { currency = c, payments = 1, contracts = [] }

Since there are no sub-Contracts I'd rather not specify that last field.

Here is a function that AND's two Contracts, c1 and c2, and returns one Contract:

and          :: Contract -> Contract -> Contract
(and) c1 c2  =  Contract { currency = undefined, payments = undefined, contracts = [c1, c2] }

I'd rather not specify those first two fields.

See why I desire optional fields? 

Perhaps I am going about it completely wrong and there is a better way to express Contract?  I am eager to learn!

/Roger



More information about the Beginners mailing list