[Haskell-cafe] ByteString.pack behavior

Don Stewart dons at galois.com
Sun May 18 16:23:10 EDT 2008


kolar:
> Hello all,
> 
>  Maybe there is something obvious I can't see, but I have this behavior 
> for 6.8.2 ghci:
> 
> $ghci ttest1p.hs
> GHCi, version 6.8.2: http://www.haskell.org/ghc/  :? for help
> Loading package base ... linking ... done.
> [1 of 1] Compiling Main             ( ttest1p.hs, interpreted )
> Ok, modules loaded: Main.
> *Main> encode' [1..100]
> Loading package array-0.1.0.0 ... linking ... done.
> Loading package bytestring-0.9.0.1 ... linking ... done.
> [1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0,12,0,13,0,... // deleted
> *Main> B.pack [0..100]
> "\NUL\SOH\STX\ETX\EOT\ENQ\ACK\a\b\t\n\v\f\r\SO\SI\DLE\DC1\DC2\DC3\DC4\NAK\SYN\ETB\CAN\EM\SUB\ESC\FS\GS\RS\US 
> !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcd"
> *Main> B.pack $ encode' [1..100]
> "*** Exception: divide by zero
> 
> where ttest1p.hs:
> 
> import qualified Data.ByteString as B
> 
> encode' [] = []
> encode' (x:xs) =
>  if x==0 then 0:0:encode' xs
>  else (x `mod` 256) : (x `div` 256) : encode' xs
> 
> 
> What is the difference, except list length and value structure? Where is 
> my error?
> 

ByteStrings take Word8 values as input, so x `div` 256 , where 256 ::
Word8, overflows to 0. 

-- Don


More information about the Haskell-Cafe mailing list