在 Haskell 中将整数转换为 Int
可能的重复:
Haskell 将整数转换为 Int?
我有一个计算birthYear 的函数,
birthYear :: Int -> Int
birthYear age = currentYear - age
currentYear :: Integral -> Integral
currentYear year = year
如何转换将 Integral 类型转换为 Int 以便birthYear 可以工作?
仅供参考,年龄固定为 Int,因为它来自 IO,我正在使用 read (age) 函数从 String 转换为 Int。
Possible Duplicate:
Haskell Convert Integer to Int?
I have a function to calculate a birthYear
birthYear :: Int -> Int
birthYear age = currentYear - age
currentYear :: Integral -> Integral
currentYear year = year
How do I cast the Integral type to an Int so birthYear can work?
FYI, age is fixed as an Int as it is coming from IO and I am using the read (age) function to convert from String to Int.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您不能使用
Integral
(这是一个类型类)作为类型。您可能的意思是:这确实有效。或者,如果您想要:
那么这也可以:
Int
是Integral
的实例,因此您不必“强制转换”任何内容。First, you cannot use
Integral
(which is a type class) as a type. You probably meant:And this just works. Or if you want to have:
Then this also works:
Int
is an instance ofIntegral
so you do not have to "cast" anything.阅读以下内容:
http://www.haskell.org/haskellwiki/Converting_numbers
从此:
Have a read of this:
http://www.haskell.org/haskellwiki/Converting_numbers
From this: