Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert GHC.Int.Int64 to Int

Tags:

haskell

I am trying to generate a random lazy ByteString of the same length as a lazy ByteString that I already have.

So I take the length of the ByteString and feed it into getEntropy like so:

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)

(using L.fromChunks . (:[]) to convert from a strict ByteString to a lazy one.)

The problem is that SE.getEntropy is of type Int -> IO ByteString while L.length is of type L.ByteString -> GHC.Int.Int64.

How can I convert an Int64 to an Int?

like image 255
DJG Avatar asked Nov 17 '14 07:11

DJG


2 Answers

You can turn any Integral type into another Num type using fromIntegral:

fromInt64ToInt :: Int64 -> Int
fromInt64ToInt = fromIntegral
like image 168
Zeta Avatar answered Nov 09 '22 03:11

Zeta


fromIntegral

In GHCI:

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

--this will error
f a
--this succeeds
f (fromIntegral a)
like image 34
ja. Avatar answered Nov 09 '22 01:11

ja.