[英]Haskell Date Parsing with Custom Separator
I'm trying to print out something like this:我试图打印出这样的东西:
Here's my code:这是我的代码:
import System.Environment
import Data.Time
main = do args <- getArgs
let year = read $ args !! 0
let month = read $ args !! 1
let day = read $ args !! 2
let greg = fromGregorian year month day
print $ showDateFormat $ toGregorian $ addDays 10 $ greg
print $ showDateFormat $ toGregorian $ addDays 100 $ greg
print $ showDateFormat $ toGregorian $ addDays 1000 $ greg
print $ showDateFormat $ toGregorian $ addDays 10000 $ greg
showDateFormat :: (Integer,Int,Int) -> String
showDateFormat (y,m,d) = y ++ "/" ++ m ++ "/" ++ d ++ "\n"
I can't figure out what's wrong.我不知道出了什么问题。
This is the error I got:这是我得到的错误:
Haskell will not implicitly convert a value of one type to another; Haskell 不会将一种类型的值隐式转换为另一种类型; you have to do such things explicitly.
你必须明确地做这些事情。 In this case, you can use
show
to convert an Int
or an Integer
to a string containing its base-10 representation.在这种情况下,您可以使用
show
将Int
或Integer
转换为包含其 base-10 表示的字符串。
showDateFormat :: (Integer,Int,Int) -> String
showDateFormat (y,m,d) =
show y ++ "/" ++
show m ++ "/" ++
show d ++ "\n"
The error message is literally telling you that it expects y
to be a value of type [Char]
(aka String
), because ++
(receiving "/":: String
as one argument) expects a String
as the other, but you have pass an Integer
value for y
instead.错误消息实际上是在告诉您它期望
y
是类型[Char]
(又名String
)的值,因为++
(接收"/":: String
作为一个参数)期望一个String
作为另一个,但你有改为为y
传递Integer
值。
(Note that String
is the expected type because it is a valid type for ++
, while Integer
is not. Otherwise, the type checker would use the left-hand argument to fix the type. [1::Int] ++ "foo"
, for example, fails because "foo"
does not have type [Int]
, not because [1]
does not have type [Char]
.) (注意
String
是预期的类型,因为它是++
的有效类型,而Integer
不是。否则,类型检查器将使用左侧参数来修复类型。 [1::Int] ++ "foo"
例如,失败是因为"foo"
没有类型[Int]
,而不是因为[1]
没有类型[Char]
。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.