[Haskell-beginners] haskell Time

Corentin Dupont corentin.dupont at gmail.com
Mon Dec 1 18:16:50 UTC 2014


Hi Claudia,
your type "Time" is good.
Here is how you'd define the functions for adding hours:

addHour :: Int -> Time -> Time
addHour myHour (hour, minute) = ((hour + myHour) `mod` 24, minute)

As can be seen in the first line, this function takes an integer where you
put the number of hours to add, takes a certain Time and then returns an
updated time.
You can try that in GHCi: put the code in a file and load it in GHCi (you
need to install Haskell platform: https://www.haskell.org/platform/).

$ ghci my_file.hs

Then you can play with your function by typing:

$ addHour 5 (10, 0)
(15, 0)

If you want a real program, you need to add some I/O:

import Data.Time

main = do
   time <- getCurrentTime
   putStrLn (show time)

You can compile this short program with:
ghc my_file.hs

Launch it with (or click on the generated executable under windows):
$ ./my_file
2014-12-01 18:12:26.989883 UTC

Tada! You have a clock.

Good luck with Haskell, it's not always easy but very rewarding language.

Corentin



On Mon, Dec 1, 2014 at 4:21 PM, Claudia Weber <kaetheweber at freenet.de>
wrote:

> Hi guys,
>
>
>
> I'm  new to Haskell and I'm just trying out some easy functions right now,
> but I'm having some trouble here..
>
>
>
> I want to display a clock
>
>
>
> type Time = (Int, Int)
>
>
>
> (hoursx,minutesy) x ={0,1,...,23} y={0,1,...,59} where I can add hours and
> minutes or set back time.
>
> If its 23,58 for example and I want to add 35 minutes, the clock has to
> set to 0,33 not 24,23.
>
>
>
> Because I'm just getting started with Haskell, I have nooo idea what to do.
>
> Would be great if someone could help me out with this :)
>
>
>
>
> ---
> Alle Postfächer an einem Ort Jetzt wechseln und E-Mail-Adresse mitnehmen! Rundum
> glücklich mit freenetMail <http://email.freenet.de/basic/Informationen>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20141201/ffabad81/attachment.html>


More information about the Beginners mailing list