Haskell 函数似乎限制整数长度 - 我认为它使用 bignums?
我这里有一个简短的haskell函数,应该将“ABCDEF”转换为0x41,0x42,0x43,0x44,0x45,0x46(它们的ascii值),然后将它们相乘,使其变为0x4142,4344,4546,但似乎限制整数长度 - 我认为 haskell 使用任意 bignums?
代码的最后一行工作正常,这让我感到困惑
有什么想法吗?非常感谢
import Data.Char
import Numeric
strToHex2 (h:[]) = ord h
strToHex2 (h:t) = (ord h) + ((strToHex2 t) * 256)
strToHex s = strToHex2 (reverse s)
main = do
print(strToHex "ABCDEF")
print ((((((((0x41*256+0x42)*256)+0x43)*256)+0x44)*256)+0x45)*256+0x46)
输出是:
1128547654 <- limited to 32 bits for some reason?
71752852194630 <- that's fine
i've got a short haskell function here that is supposed to convert "ABCDEF" into 0x41,0x42,0x43,0x44,0x45,0x46 (their ascii values), then multiply them so it becomes 0x4142,4344,4546 but it seems to be limiting integer length - i thought haskell used arbitrary bignums?
The last line of the code works fine, which puzzles me
Any ideas? Thanks a lot
import Data.Char
import Numeric
strToHex2 (h:[]) = ord h
strToHex2 (h:t) = (ord h) + ((strToHex2 t) * 256)
strToHex s = strToHex2 (reverse s)
main = do
print(strToHex "ABCDEF")
print ((((((((0x41*256+0x42)*256)+0x43)*256)+0x44)*256)+0x45)*256+0x46)
The output is:
1128547654 <- limited to 32 bits for some reason?
71752852194630 <- that's fine
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是
ord
返回一个Int
,它是固定宽度的。您需要toInteger $ ord h
。Your problem is that
ord
returns anInt
, which is fixed-width. You wanttoInteger $ ord h
.