简体   繁体   中英

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; 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.

showDateFormat :: (Integer,Int,Int) -> String
showDateFormat (y,m,d) =  y ++ "/" ++  m ++ "/" ++  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.

(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] .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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