andro
andro

Reputation: 939

Current time in IO monad

If I have the following, for example:

import Data.Time.Clock.POSIX

t = getPOSIXTIME

Then t :: IO POSIXTime. That means it is in the IO monad, this much I understand. Is there any way to get the value out of the monad to use in other functions in the program? I don't want to output the value to the terminal.

I apologise for such a newbie question, but the more monad tutorials I read the less I understand any of it. This is essentially more a question about monads than specifically about time.

Upvotes: 2

Views: 373

Answers (1)

Yuan Wang
Yuan Wang

Reputation: 479

If your other functions expect POSIXTime, you can wrap them inside do notation.

The easiest way is probably to put it in main. Assuming your other function is named f then:

import Data.Time.Clock.POSIX

main :: IO ()
main = do
    t <- getPOSIXTIME
    f t

e.g.

ghc time XXX.hs
./time

t inside the do notation is POSIXTime, not IO POSIXTime.

http://en.wikibooks.org/wiki/Haskell/do_Notation

Upvotes: 6

Related Questions