How i use GHC.Word.Word8 wit Int ?

Hi, friends rollDice :: Word8 -> IO Word8 rollDice n = do bracket (openFile "/dev/random" ReadMode) (hClose) (\hd -> do v <- fmap B.unpack (B.hGet hd 1) let v1 = Data.List.head v return $ (v1 `mod` n) + 1) ..... blueIdx <- rollDice $ length [1..33] Couldn't match expected type `Word8' against inferred type `Int' In the second argument of `($)', namely `length yesBlue I know "length [1..33]" is Int not Word8, but Word8 is enough here. Sincerely!

2009/5/20 z_axis@163.com
Hi, friends
rollDice :: Word8 -> IO Word8 rollDice n = do bracket (openFile "/dev/random" ReadMode) (hClose) (\hd -> do v <- fmap B.unpack (B.hGet hd 1) let v1 = Data.List.head v return $ (v1 `mod` n) + 1) ..... blueIdx <- rollDice $ length [1..33]
Couldn't match expected type `Word8' against inferred type `Int' In the second argument of `($)', namely `length yesBlue
I know "length [1..33]" is Int not Word8, but Word8 is enough here. Sincerely!
You can use fromIntegral to convert Integral types to other numeric types: fromIntegral :: (Integral a, Num b) => a -> b Prelude Data.Word> (fromIntegral (3 :: Int)) :: Word8 3 Watch out for overflow. Cheers, Bernie.

On Wed, May 20, 2009 at 3:40 AM, z_axis@163.com
Hi, friends
rollDice :: Word8 -> IO Word8 rollDice n = do bracket (openFile "/dev/random" ReadMode) (hClose) (\hd -> do v <- fmap B.unpack (B.hGet hd 1) let v1 = Data.List.head v return $ (v1 `mod` n) + 1) ..... blueIdx <- rollDice $ length [1..33]
Couldn't match expected type `Word8' against inferred type `Int' In the second argument of `($)', namely `length yesBlue
I know "length [1..33]" is Int not Word8, but Word8 is enough here. Sincerely!
Word8 is an instance of Num, so you could use Data.List.genericLength, which has the type: genericLength :: Num i => [b] -> i http://haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html#26 -- Johannes Laire
participants (4)
-
Bernie Pope
-
Felipe Lessa
-
Johannes Laire
-
z_axis@163.com