Two parameter memoization in Haskell

前端 未结 4 1607
盖世英雄少女心
盖世英雄少女心 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:03

    Here is a version using Data.MemoTrie from the MemoTrie package to memoize the function:

    import Data.MemoTrie(memo2)
    
    gridwalk :: Int -> Int -> Int
    gridwalk = memo2 gw
      where
        gw 0 _ = 1
        gw _ 0 = 1
        gw x y = gridwalk (x - 1) y + gridwalk x (y - 1)
    

提交回复
热议问题