[Haskell-cafe] Typing question

Ketil Malde ketil+haskell at ii.uib.no
Tue Jan 11 05:40:41 EST 2005


Dmitri Pissarenko <mailing-lists at dapissarenko.com> writes:

> a) How should I define the types of the attributes correctly?

  data Purchase = P Double Double
  data Customer = C Int [Purchase]

or, if you want named fields:

  data Purchase = P { price, rebate :: Double }
  data Customer = C { id :: Int, purchases :: [Purchase] }

> b) Is it possible to impose a constraint on the "constructor" of
> tuple Purchase, so that the compiler throws an error, if one tries
> to create a purchase where rebate is greater than price (rebate and
> price are given in absolute numbers; rebate is NOT a fraction of
> price)?

One way would be to store it as a fraction, e.g. using an Int8 with
implicit 256 (or 255) denominator, which elminates the problem.  Or
create a function that constructs only valid Purchases, and hide the P
constructor (i.e. don't export it from the module). 

> c) What is a correct name for "attributes" of tuples (such as price
> and rebate in Purchase) and "constructors" of tuples?

I think the accepted lingo is "records with named fields". 

-kzm
-- 
If I haven't seen further, it is by standing in the footprints of giants



More information about the Haskell-Cafe mailing list