
On Mon, May 18, 2009 at 10:13 PM, Brandon S. Allbery KF8NH
On May 19, 2009, at 01:07 , z_axis wrote:
rollDice_t n = do hd <- openFile "/dev/random" ReadMode v <- B.hGet hd 1 return (v `mod` n) + 1
No instance for (Integral B.ByteString)
You can't just read a binary string and have it interpreted as a number; you want to use Data.Binary to extract an Int (or whatever) from the ByteString. The same would apply with legacy Strings; in Haskell, String, ByteString, and Int are distinct types and there is no automatic casting. In fact I'm not quite sure why you thought that should work; even Perl would make you unpack(), and C would require you to use an appropriately-aligned buffer and unsafely cast the (char *) to an (int *).
I just want to add that, this wouldn't stop Visual Basic! I've often seen this bit of scary code in VB: Dim i as Integer = 5 If i = "5" Then ' Do something, because 5 = "5" End If The designers of VB were quite odd though. I frequently find that VB violates the assumptions I make about programming languages (the logical "And" operation isn't short-circuit logic!). Jason