(no subject)

Hal Daume III hdaume@ISI.EDU
Sun, 24 Feb 2002 23:06:29 -0800 (PST)


I'm not sure where the documentation is, but here's the idea on how to
used named fields.  I'll make a smaller example, though :)

> data T = T { x :: Int, y :: Bool }

now, to create a value of type T, you can write:

> x = T 5 True

as the datatype declaration creates the following function:

> T :: Int -> Bool -> T

You can also pattern match as if you had just declared it as "data T = T
Int Bool".  However, this datatype declaration introduces two functions
into the namespace:

> x :: T -> Int
> y :: T -> Bool

which extract values.  for instance:

> x (T 5 True)

evaluates to 5 and

> y (T 5 True)

evaluates to True.

You can also use field names to create values:

> T { x = 5, y = True }

or

> T { y = True, x = 5}

order is irrelevant (i'm not sure about rules if you specify the same
field more than once...check in the report on that).

Finally, you can update parts of labelled fields independent of everything
else:

> let q = T {x=4, y=True}
>     q' = q {x=5}
> in  q'

will yield T 5 True.

Of course, this isn't value replacement, it's just a more convenient way
to create a new value based on an old one, with minor changes.

I see Ashley also replied to this thread with a pointer to
documentation.  Hopefully the combination will help.

--
Hal Daume III

 "Computer science is no more about computers    | hdaume@isi.edu
  than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume

On 25 Feb 2002, Tom Bevan wrote:

> 
> 
> Hi,
> 
> I've come across this sort of data constructor below many times but I'm
> not really sure how to use it. Can someone please point me to the right
> section in the documentation?
> In particular, I want to know how to create a calendar time and how to
> access the fields .
> 
> Tom
> 
> data CalendarTime = CalendarTime {
> 		ctYear   :: Int,
>         	ctMonth  :: Month,
>         	ctDay, ctHour, ctMin, ctSec  :: Int,
> 		ctPicosec :: Integer,
> 		ctWDay    :: Day,
> 		ctYDay       :: Int,
> 		ctTZName   :: String,
> 		ctTZ          :: Int,
> 		ctIsDST :: Bool
> 	} deriving (Eq, Ord, Read, Show)
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>