简体   繁体   English

Haskell函数将日期的一部分作为字符串

[英]Haskell function to get part of date as string

I have a beginner question about dates and String in Haskell. 我有一个关于Haskell中的日期和String的初学者问题。

I need to get part of date (year, month or day) as String in Haskell. 我需要在Haskell中将日期(年,月或日)的一部分作为String I found out, that if I write the following two lines in GHCi 我发现,如果我在GHCi中写下以下两行

Prelude> now <- getCurrentTime
Prelude> let mon = formatTime defaultTimeLocale "%B" now

then mon is of type String . 然后monString类型。 However, I am unable to put this in a function. 但是,我无法将其置于一个功能中。 I tried for instance the following: 我试过以下内容:

getCurrMonth = do
    now <- getCurrentTime
    putStrLn (formatTime defaultTimeLocale "%B" now)

But this returns type IO () and I need String (also not IO String , only String ). 但这会返回类型IO () ,我需要String (也不是IO String ,只有String )。

I understand that do statement creates a monad, which I don't want, but I have been unable to find any other solution for getting date in Haskell. 我知道do语句会创建一个我不想要的monad,但是我无法找到任何其他解决方案来获取Haskell中的日期。

So, is there any way to write a function like this? 那么,有没有办法写这样的函数?

Thanks in advance for any help! 在此先感谢您的帮助!

If you want to return a String representing the current time, it will have to be in the IO monad, as the value of the current time is always changing! 如果你想返回一个字符串表示当前时间,就必须要在IO单子,由于当前时间的值总是在变化!

What you can do is to return a String in the IO monad: 你可以做的是在IO monad中返回一个String:

> getCurrMonth :: IO String
> getCurrMonth = do
>    now <- getCurrentTime
>    return (formatTime defaultTimeLocale "%B" now)

then, from your top level (eg in main), you can pass the String around: 然后,从你的顶层(例如在main中),你可以传递String:

> main = do
>     s <- getCurrMonth
>     ... do something with s ...

If you really want a pure function of that sort, then you need to pass in the time explicitly as a parameter. 如果你真的想要那种纯函数,那么你需要明确地传递时间作为参数。

import System.Locale (defaultTimeLocale)
import System.Time (formatCalendarTime, toUTCTime, getClockTime, ClockTime)

main = do now <- getClockTime
          putStrLn $ getMonthString now

getMonthString :: ClockTime -> String
getMonthString = formatCalendarTime defaultTimeLocale "%B" . toUTCTime

Notice how getMonthString can be pure since the IO action getClockTime is performed elsewhere. 注意getMonthString如何是纯粹的,因为IO动作getClockTime在别处执行。

I used the old-time functions, because I was testing it out on codepad , which apparently doesn't have the newer time package. 我使用了旧时功能,因为我在键盘上测试它 ,显然没有更新的时间包。 :( I'm new to the old time functions so this might be off a couple hours since it uses toUTCTime . :(我是旧时功能的新手所以这可能会因为它使用toUTCTime而关闭几个小时。

As Don said, there's no way to avoid using monads in this situation. 正如唐所说,在这种情况下,没有办法避免使用monad。 Remember that Haskell is a pure functional language, and therefore a function must always return the same output given a particular input. 请记住,Haskell是一种纯函数式语言,因此函数必须始终在给定特定输入的情况下返回相同的输出。 Haskell.org provides a great explanation and introduction here that is certainly worth looking at. Haskell.org提供了一个很好的解释和介绍这里那肯定是值得考虑的。 You'd also probably benefit from monad introduction like this one or a Haskell I/O tutorial like this one . 你也很可能从单子一样受益引进这一个或类似Haskell的I / O教程这一个 Of course there are tons more resources online you can find. 当然,你可以找到更多的在线资源。 Monads can initially be daunting, but they're really not as difficult as they seem at first. Monads最初可能令人生畏,但它们并不像最初看起来那么困难。

Oh, and I strongly advise against using unsafePerformIO . 哦,我强烈建议不要使用unsafePerformIO There's a very good reason it has the word "unsafe" in the name, and it was definitely not created for situations like this. 有一个很好的理由,它在名称中有“不安全”一词,而且绝对不是为这种情况创建的。 Using it will only lead to bad habits and problems down the line. 使用它只会导致不良习惯和问题。

Good luck learning Haskell! 祝学习Haskell好运!

You can't get just a String, it has to be IO String. 你不能只获得一个String,它必须是IO String。 This is because getCurrMonth is not a pure function, it returns different values at different times, so it has to be in IO. 这是因为getCurrMonth不是纯函数,它在不同的时间返回不同的值,因此它必须在IO中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM