将GHC.Int.Int64转换为Int

时间:2014-11-17 07:03:32

标签: haskell

我正在尝试生成一个与我已经拥有的懒惰ByteString长度相同的随机懒惰ByteString

所以我采用ByteString的长度并将其输入getEntropy,如下所示:

import qualified Data.ByteString.Lazy.Char8 as L
import qualified System.Entropy as SE

string :: L.ByteString
string = L.pack "Hello world!"

randomString :: IO L.ByteString
randomString = L.fromChunks . (:[]) <$> SE.getEntropy (L.length string)

(使用L.fromChunks . (:[])从严格ByteString转换为惰性{。}

问题是SE.getEntropy的类型为Int -> IO ByteString,而L.length的类型为L.ByteString -> GHC.Int.Int64

如何将Int64转换为Int

2 个答案:

答案 0 :(得分:10)

您可以使用fromIntegral将任何Integral类型转换为其他Num类型:

fromInt64ToInt :: Int64 -> Int
fromInt64ToInt = fromIntegral

答案 1 :(得分:4)

fromIntegral

GHCI:

let a = 6 :: GHC.Int.Int64
let f :: Int -> Int; f x = x;

--this will error
f a
--this succeeds
f (fromIntegral a)