Ackermann very inefficient with Haskell/GHC

后端 未结 7 918
小鲜肉
小鲜肉 2020-12-23 13:58

I try computing Ackermann(4,1) and there\'s a big difference in performance between different languages/compilers. Below are results on my

7条回答
  •  遥遥无期
    2020-12-23 14:01

    Writing the algorithm in Haskell in a way that looks similar to the way you wrote it in C is not the same algorithm, because the semantics of recursion are quite different.

    Here is a version using the same mathematical algorithm, but where we represent calls to the Ackermann function symbolically using a data type. That way, we can control the semantics of the recursion more precisely.

    When compiled with optimization, this version runs in constant memory, but it is slow - about 4.5 minutes in an environment similar to yours. But I'm sure it could be modified to be much faster. This is just to give the idea.

    data Ack = Ack !Int
    
    ack :: Int -> Int -> Int
    ack m n = length . ackR $ Ack m : replicate n (Ack 0)
      where
        ackR n@(Ack 0 : _) = n
        ackR n             = ackR $ ack' n
    
        ack' [] = []
        ack' (Ack 0 : n) = Ack 0 : ack' n
        ack' [Ack m]     = [Ack (m-1), Ack 0]
        ack' (Ack m : n) = Ack (m-1) : ack' (Ack m : decr n)
    
        decr (Ack 0 : n) = n
        decr n           = decr $ ack' n
    

提交回复
热议问题