Reputation: 549
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:
Upvotes: 0
Views: 72
Reputation: 532238
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) = 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.
(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]
.)
Upvotes: 1