1

I tried to convert Haskell code that calculates the Adler-32 hash of 'a' into Frege but got 6422626 instead of 300286872

Excerpt from the Haskell code at http://book.realworldhaskell.org/read/functional-programming.html

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

Excerpt from the Frege code at 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

Is the choice of operators wrong or the signed/unsigned 32/64 integer properties?

Dierk
  • 1,308
  • 7
  • 13
  • 1
    This is a package I wanted to have ported from Haskell for some time, as the different operators are indeed needlessly confusing. Any volunteers? – Ingo Oct 08 '14 at 16:42
  • I will do it, Ingo. I am working on porting java.util but I can hold on that since I have to do few more things before I can push it out. – Marimuthu Madasamy Oct 08 '14 at 17:29

1 Answers1

3

There is nothing wrong with the code. The input according to wikipedia (found it from your linked source) for that output should be 'Wikipedia':

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
Marimuthu Madasamy
  • 13,126
  • 4
  • 30
  • 52
  • oh, thanks, I was totally misreading the Wikipedia article. Added your answer to the commit message https://github.com/Dierk/Real_World_Frege/commit/315fd6ad142b243907540b54c7b30b0b98f7c227 – Dierk Oct 07 '14 at 22:35