[Haskell-beginners] using the current time

Daniel Fischer daniel.is.fischer at web.de
Tue Dec 8 18:39:38 EST 2009


Am Mittwoch 09 Dezember 2009 00:18:54 schrieb Torsten Otto:
> Hi!
>
> 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?")]

Unnecessarily verbose!!!

                                   (["Moin"], "Und?")


>
> (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"

But that's wrong, it must be "Sonnabend", otherwise you're speaking Austrian or Bavarian, 
not German.

> >        | (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?

Since the chatbot probably works in IO anyway, is it an option to have the second 
components of type IO String in general?

If that is not possible, sacrifice a few virgins, spit over your left shoulder and 
contemplate using unsafePerformIO.

>
> Regards,
> Torsten




More information about the Beginners mailing list