[Haskell-beginners] using the current time

Torsten Otto t-otto-news at gmx.de
Tue Dec 8 18:18:54 EST 2009


We are trying to build a chatbot that can among many other things tell the partner what the current time is. Now obviously current time and pure functional programming don't mix well, but Haskell CAN do it. We have played around with it for a while and have figured out quite a bit, but the final icing is missing on the cake.

It would be cool to have a dictionary like so
> wissen = [

                                Einfache Begrüßung -> Frage nach dem Wohlbefinden

>                                (["hallo", "hi", "hey"],      "Hi, wie geht's?"),
>                                (["guten tag", "tag"],       "Wie geht es dir?")]

(yes, the bot speaks German!) that could include a line like

>				(["Uhr","Zeit","spät"],	time)

where time is a function evaluating to a String including the current time.

Once we have the time, we can get it translated into German by

> formatiereZeit :: ClockTime -> String
> formatiereZeit t 
>                                 | (head(words (calendarTimeToString (toUTCTime t))) == "Mon") = "Montag"
>                                 | (head(words (calendarTimeToString (toUTCTime t))) == "Tue") = "Dienstag"
>                                 | (head(words (calendarTimeToString (toUTCTime t))) == "Wed") = "Mittwoch"
>                                 | (head(words (calendarTimeToString (toUTCTime t))) == "Thu") = "Donnerstag"
>                                 | (head(words (calendarTimeToString (toUTCTime t))) == "Fri") = "Freitag"
>                                 | (head(words (calendarTimeToString (toUTCTime t))) == "Sat") = "Samstag"
>                                 | (head(words (calendarTimeToString (toUTCTime t))) == "Sun") = "Sonntag"
>                                 | otherwise ="keine Uhr heute"

which results in a string. Now how do we access the system time in a useful way?

> time = formatiereZeit (NOW WHAT?)

Would the current time step out of the IO monad, please?


More information about the Beginners mailing list