I have below code to take the args to set some offset time.
setOffsetTime :: (Ord a, Num b)=>[a] -> b
setOffsetTime [] = 200
setOffsetTime (x:xs) = read x::Int
But compiler says "Could not deduce (b ~ Int) from the context (Ord a, Num b) bound by the type signature for setOffsetTime :: (Ord a, Num b) => [a] -> b
Also I found I could not use 200.0 if I want float as the default value. The compilers says "Could not deduce (Fractional b) arising from the literal `200.0'"
Could any one show me some code as a function (not in the prelude) that takes an arg to store some variable so I can use in other function? I can do this in the main = do, but hope to use an elegant function to achieve this. Is there any global constant stuff in Hasekll? I googled it, but seems not.
I wanna use Haskell to replace some of my python script although it is not easy.
setOffsetTime
you explicitly state that the result ofread x
should be anInt
. This immediately implies that the result type of your function isInt
and not the more generalb
forNum b
. And that is exactly what the compiler tells you (in his own words). – chrisread
isRead a => String -> a
, hence its argument (x
in your case) must be of typeString
, but you are usinga
forOrd a
. – chrissetOffsetTime
and just have to correct some types. E.g., drop theOrd a
contraint and replace[a]
with[String]
. Further dropNum b
and replaceb
byFloat
. And also drop... :: Int
from the last line. – chris