
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?")]
(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? Regards, Torsten

Excerpts from Torsten Otto's message of Tue Dec 08 18:18:54 -0500 2009:
time = formatiereZeit (NOW WHAT?)
Would the current time step out of the IO monad, please?
Unfortunately, there is no way to "step out" of the IO monad: once in the IO monad, always in the IO monad. However, if you let time be of type IO String instead of String, you can do: time = (liftM formatiereZeit) getClockTime Where liftM :: (a -> b) -> m a -> m b, i.e. it converts your function ClockTime -> String into IO ClockTime -> IO String. Cheers, Edward

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
participants (3)
-
Daniel Fischer
-
Edward Z. Yang
-
Torsten Otto