I have implemented the LEB128 encoding for unsigned integers. I'm fairly new to Haskell so I have some doubt about the coding style I'm using, particularly the definition of byte
and then 2 lines later byte'
:
import Data.Bits
import Data.Word
uleb128 :: Integer -> [Word8]
uleb128 0 = []
uleb128 n =
let byte = (fromIntegral n :: Word8) .&. 0x7F
n' = shiftR n 7
byte' = if n' == 0
then byte
else (byte .|. 0x80)
in
byte' : uleb128 n'
Futhermore I have doubts about the choice of let .. in
in favor of ... where
and the use of if then else
vs case of
. I'm very much still learning Haskell so I'm curious how a seasoned Haskell programmer would have written the above function.