[英]Haskell Date Parsing with Custom Separator
我試圖打印出這樣的東西:
這是我的代碼:
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"
我不知道出了什么問題。
這是我得到的錯誤:
Haskell 不會將一種類型的值隱式轉換為另一種類型; 你必須明確地做這些事情。 在這種情況下,您可以使用show
將Int
或Integer
轉換為包含其 base-10 表示的字符串。
showDateFormat :: (Integer,Int,Int) -> String
showDateFormat (y,m,d) = show y ++ "/" ++ show m ++ "/" ++ show d ++ "\n"
錯誤消息實際上是在告訴您它期望y
是類型[Char]
(又名String
)的值,因為++
(接收"/":: String
作為一個參數)期望一個String
作為另一個,但你有改為為y
傳遞Integer
值。
(注意String
是預期的類型,因為它是++
的有效類型,而Integer
不是。否則,類型檢查器將使用左側參數來修復類型。 [1::Int] ++ "foo"
例如,失敗是因為"foo"
沒有類型[Int]
,而不是因為[1]
沒有類型[Char]
。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.