luqui / data-memocombinators

Pure memoization combinators for Haskell
37 stars 7 forks source link

memoize (Int, Int) not working? #12

Open antifermion opened 5 years ago

antifermion commented 5 years ago

I would expect the following functions to all have quadratic running time, however the first one seems to be exponential. Am I doing anything wrong?

import Data.MemoCombinators.Class
import Data.MemoCombinators
import System.IO

test :: (Int, Int) -> Int
test = memoize test' where
  test' :: (Int, Int) -> Int
  test' (a, b)
    | a == b = a
    | a < b = test (a+1, b) + test (a, b-1)
    | a > b = 0

test2 :: Int -> Int -> Int
test2 = memo2 integral integral test2' where
  test2' a b
    | a == b = a
    | a < b = test2 (a+1) b + test2 a (b-1)
    | a > b = 0

test3 :: (Int, Int) -> Int
test3 = pair integral integral test3' where
  test3' (a, b)
    | a == b = a
    | a < b = test3 (a+1, b) + test3 (a, b-1)
    | a > b = 0

main :: IO()
main = do
  hSetBuffering stdout NoBuffering
  print $ test2 0 25
  print $ test3 (0, 25)
  print $ test (0, 25)
luqui commented 5 years ago

Last I remember I had found a major problem in the approach of Data.MemoCombinators.Class, though I don't remember enough of it. I need to formally deprecate it. Thanks for the report.

luqui commented 5 years ago

No I think I'm wrong, I just did it wrong. I shall fix this. Thanks again.