[Haskell-cafe] ByteString.pack behavior

Dusan Kolar kolar at fit.vutbr.cz
Mon May 19 00:25:42 EDT 2008


Thanks. I've realized that as soon as I was in my bed. ;-)

Nevertheless, a question comes to me - shouldn't compiler report a 
warning? I know it cannot in the current state, but it should. :-) Quite 
dummy C compilers tell me I'm "loosing significant digits of number 
literals" if I'm doing that. Maybe, already seen in some other thread 
some time ago, the compiler should be less general/should know more 
about data types...

Thanks and regards

Dusan

Don Stewart wrote:
> 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
>   

-- 

 Dusan Kolar            tel: +420 54 114 1238
 UIFS FIT VUT Brno      fax: +420 54 114 1270
 Bozetechova 2       e-mail: kolar at fit.vutbr.cz
 Brno 612 66
 Czech Republic

--



More information about the Haskell-Cafe mailing list