On Thu, 2006-06-15 at 12:43 +0100, Vladimir Portnykh wrote:
> Suppose there is a data definition in Haskell:
> data MyType = MyType { date   :: Double,
>                  weight       :: Double,
>                 height        :: Double
>               } deriving (Eq, Ord, Show)
> 
> Is it possible to check if the field height, for example, is filled 
> in(defined)? Can we give default values in Haskell?

There is no implicit null, standard technique is to use an explicit null
using the Maybe type:

data MyType = MyType {
  date   :: Maybe Double,
  weight :: Maybe Double,
  height :: Maybe Double
} deriving (Eq, Ord, Show)

The Maybe type is defined like this:

data Maybe a = Nothing | Just a

so a value of type 'Maybe a' can be either Just x or Nothing.

So you can represent your lack of a value using Nothing. You can get
something similar to default values in a record by using the ordinary
record update syntax. Suppose we start with a default record value:

default :: MyType
default = MyType { date = Nothing, weight = Nothing, height = Nothing }

then you can construct your records using:

foo = default { weight = 3.2 }

so foo will get all the default values except for the ones you
explicitly set.

Duncan

_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to