do we have something like isDefined or isNull in Haskell?

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? Many thanks and sorry fro so sily questions. Vladimir _________________________________________________________________ The new MSN Search Toolbar now includes Desktop search! http://join.msn.com/toolbar/overview

On 6/15/06, Vladimir Portnykh
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?
Many thanks and sorry fro so sily questions. Vladimir
You could make date, weight, and height "Maybe Double"s, then isDefined = isJust and isNull = isNothing. Bryan

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

On Thu, 2006-06-15 at 13:11 +0100, Duncan Coutts wrote:
then you can construct your records using:
foo = default { weight = 3.2 }
Oops, as David House pointed out to me that should of course be foo = default { weight = Just 3.2 } Duncan

Duncan Coutts wrote:
On Thu, 2006-06-15 at 13:11 +0100, Duncan Coutts wrote:
then you can construct your records using:
foo = default { weight = 3.2 }
Oops, as David House pointed out to me that should of course be
foo = default { weight = Just 3.2 }
I think the correct response in these cases is: That was left as an exercise for the type checker. On another note, who picked the word `Just' for this type and how did we end up with Some x | None in O'Caml and Just x | Nothing in Haskell?

On Thursday, June 15, 2006 8:07 PM Clifford Beshers wrote:
On another note, who picked the word `Just' for this type and how did we end up with Some x | None in O'Caml and Just x | Nothing in Haskell?
I've always thought this is one of the most charming things about Haskell, along with the use of the quaint word "otherwise". It's so friendly and conversational compared to the cold logicality of OCaml or the shoutyness of SML's SOME and NONE eg a phone call to a relative: callee: "Who's that?" caller: "Don't worry, it's Just me!" at school: teacher: "What did you just say?" pupil: "Nothing" in a shop: Haskeller: "I'll just buy some crisps" OCamler: "I'll buy some packet of crisps" SMLer: "I'll buy SOME packet of crisps" Regards, Brian. -- Logic empowers us and Love gives us purpose. Yet still phantoms restless for eras long past, congealed in the present in unthought forms, strive mightily unseen to destroy us. http://www.metamilk.com
participants (5)
-
Brian Hulley
-
Bryan Burgers
-
Clifford Beshers
-
Duncan Coutts
-
Vladimir Portnykh