[Haskell-cafe] Basic question concerning data constructors
Bulat Ziganshin
bulat.ziganshin at gmail.com
Sun Dec 30 09:51:55 EST 2007
Hello Joost,
Sunday, December 30, 2007, 5:24:59 PM, you wrote:
> data ClockTime = TOD Integer Integer
it declares type with name ClockTime (which you may use on type
signatures, other type declarations and so on) with one constructor
TOD accepting two Integer values. the only way to construct value of
this type is to apply TOD to two Integer expressions (believe it or
not but this declaration automatically defines TOD as function with
the following signature:
TOD :: Integer -> Integer -> ClockTime
f.e.:
seconds2clockTime :: Double -> ClockTime
seconds2clockTime s = TOD (floor(s)) (round(s*1e12)
the only way to deconstruct values of this type is to use TOD
constructor in parser matching, f.e.:
clockTime2seconds :: ClockTime -> Double
clockTime2seconds (TOD s p) = fromInteger(s) + fromInteger(p)/1e12
--
Best regards,
Bulat mailto:Bulat.Ziganshin at gmail.com
More information about the Haskell-Cafe
mailing list