弗雷格的二进制操作与Haskell不同?

时间:2014-10-07 16:09:45

标签: haskell frege

我尝试转换Haskell代码来计算' a'的Adler-32哈希值。进入弗雷格但获得了6422626而不是300286872

摘自http://book.realworldhaskell.org/read/functional-programming.html

的Haskell代码
adler32_try2 xs = helper (1,0) xs
  where helper (a,b) (x:xs) =
          let a' = (a + (ord x .&. 0xff)) `mod` base
              b' = (a' + b) `mod` base
          in helper (a',b') xs
        helper (a,b) _ = (b `shiftL` 16) .|. a

摘自https://github.com/Dierk/Real_World_Frege/blob/master/realworld/chapter4/G_Reducing.fr

的弗雷格代码
adler32 xs = accuAdler (1,0) xs where
    accuAdler (a,b) (y:ys) =
        let newA = (a + (ord y `band` 0xff)) `mod` base
            newB = (newA + b) `mod` base
        in  accuAdler (newA, newB) ys
    accuAdler (a,b) _ = (b `bshl` 16) `bor` a

运算符的选择是错误的还是有符号/无符号的32/64整数属性?

1 个答案:

答案 0 :(得分:3)

代码没有问题。根据维基百科(从您的链接来源找到)输出的输入应为“维基百科”:

frege> base = 65521
value base :: Int

frege> :{
> adler32 xs = accuAdler (1,0) xs where
    accuAdler (a,b) (y:ys) =
        let newA = (a + (ord y `band` 0xff)) `mod` base
            newB = (newA + b) `mod` base
        in  accuAdler (newA, newB) ys
    accuAdler (a,b) _ = (b `bshl` 16) `bor` a
> :}
function adler32 :: Enum α => [α] -> Int

frege> adler32 "Wikipedia".toList
300286872