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

David Virebayre dav.vire+haskell at gmail.com
Mon Jul 18 09:15:45 CEST 2011


2011/7/18 Yitzchak Gale <gale at sefer.org>:
> Roger L. Costello wrote:
>>> data Contract =
>>>   Contract {
>>>              currency  :: Currency
>>>            , payments  :: Double
>>>            , contracts :: [Contract]
>>>            }
>>>  deriving (Show)
>
> David Virebayre wrote:
>> I'm not sure I would model your datatype this way, I don't like the
>> idea to put unnecessary undefined values in the case of subcontracts.
>>
>> data Contract = Contract  { currency :: Currency, payments :: Double }
>>              | SubContract { contracts :: [Contract] }
>
> I think Roger's original design is just fine.
>
> There is nothing "undefined" about the empty list. Roger is
> saying that every contract has a list of subcontracts. Lists can
> be empty, and that is legitimate.

I'm not sure you read his post entirely.

Here's his and function :

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

Notice how he's setting currency and payments to undefined, since he
needs only the contracts field for subcontracts.

David.



More information about the Beginners mailing list