[Haskell-cafe] ByteString.pack behavior
Miguel Mitrofanov
miguelimo38 at yandex.ru
Sun May 18 16:24:44 EDT 2008
Yes, this is obvious
B.pack :: [GHC.Word.Word8] -> B.ByteString
So, in
B.pack $ encode' [1..100]
GHCi expects "encode' [1..100]" to be of type [GHC.Word.Word8]
Now, your encode' has type
encode' :: Integral a => [a] -> [a]
So, in encode' definition, all calculations are in Word8.
Now, (256 :: Word8) is zero. No wonder you get a "divide by zero"
error when trying to calculate "something `mod` 256"
On 19 May 2008, at 00:14, Dušan Kolář wrote:
> 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?
>
> Thanks for any hint,
>
> Dusan
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
More information about the Haskell-Cafe
mailing list