do we have something like isDefined or isNull in Haskell?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

do we have something like isDefined or isNull in Haskell?

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

_________________________________________________________________
The new MSN Search Toolbar now includes Desktop search!
http://join.msn.com/toolbar/overview

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: do we have something like isDefined or isNull in Haskell?

Bryan Burgers
On 6/15/06, Vladimir Portnykh <[hidden email]> 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?
>
> 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
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: do we have something like isDefined or isNull in Haskell?

Duncan Coutts
In reply to this post by Vladimir Portnykh
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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: do we have something like isDefined or isNull in Haskell?

Duncan Coutts
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

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: do we have something like isDefined or isNull in Haskell?

Clifford Beshers
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?


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: do we have something like isDefined or isNull in Haskell?

Bryan Burgers
In reply to this post by Bryan Burgers
Vladimir,

I think you forgot to put Haskell-cafe as a recipient of this email,
so first I'll repost what you wrote.

On 6/15/06, Vladimir Portnykh <[hidden email]> wrote:

> many thanks. i have the follwoing code:
>
> module MyType (DataContainer(..)) where
> import Maybe
> data DataContainer =
>     MyType {
>             date :: [Maybe Double],
>                         weight ::[Maybe Double],
>                         height ::[Maybe Double]
>            }
>             deriving (Eq, Ord, Show)
>
> myType =  MyType {
>             date =
> [38548,38562,38576,38590,38604,38618,38632,38646,38660,38674],
>                         weight = [],
>                         height = []
>                         }
>
> reportProblem = if isJust (height myType  !! (length (date myType) - 1))
> then 0 else 1
>
>
> it failed to compiled saying "No instance for (Num (Maybe Double)) arising
> from the literal 38674 .

After reading your code for a while, I still don't know if I quite
understand.  Can you explain exactly what you are trying to do?

My thought is that maybe you just want a list of objects, since you
are indexing height at the same index as date.  Then you just want a
list of the structures that Duncan described.

myObjects = [default {weight = 3.2}, default {weight = 7.9, date=38458},...]

If not, hopefully somebody a little more knowledgable than I can help you.

Bryan
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: do we have something like isDefined or isNullin Haskell?

Brian Hulley
In reply to this post by Clifford Beshers
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

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