Two parameter memoization in Haskell

前端 未结 4 1635
盖世英雄少女心
盖世英雄少女心 2020-12-15 07:45

I\'m trying to memoize the following function:

gridwalk x y
    | x == 0 = 1
    | y == 0 = 1
    | otherwise = (gridwalk (x - 1) y) + (gridwalk x (y - 1))
<         


        
4条回答
  •  忘掉有多难
    2020-12-15 08:08

    Use the data-memocombinators package from hackage. It provides easy to use memorization techniques and provides an easy and breve way to use them:

    import Data.MemoCombinators (memo2,integral)
    
    gridwalk = memo2 integral integral gridwalk' where
      gridwalk' x y
        | x == 0 = 1
        | y == 0 = 1
        | otherwise = (gridwalk (x - 1) y) + (gridwalk x (y - 1))
    

提交回复
热议问题