我怎样才能重写这个haskell函数的复杂性

时间:2016-08-23 18:39:33

标签: haskell

c:: Integer -> Integer
c n | n < 3 = n + 100
    | otherwise = c (n-2) + c (n-3) + c (n-2)

这种形式的c的复杂性是指数的。重写函数c,使其复杂度呈线性。 如果1000

1 个答案:

答案 0 :(得分:6)

使用Data.Memocombinators

import Data.MemoCombinators

c:: Integer -> Integer
c n | n < 3 = n + 100
    | otherwise = c' (n-2) + c' (n-3) + c' (n-2)

c' = integral c

main = print $ c' 1000

请注意c如何重新编写c'来调用c的备忘版本。

相关问题