[英]Haskell Data Structure
I am trying to build a data-structure in Haskell which functions can use to avoid re-computing values. 我正在尝试在Haskell中建立一个数据结构,该函数可以用来避免重新计算值。 For example, say I had the function:
例如,假设我具有以下功能:
f :: Int -> Int -> Int
f 1 1 == 1
f m n
| abs m > n = 0
| OTHERWISE if value of f m n has already been computed by another recursive branch, return that value and add it to the "database"
| OTHERWISE return f (m-1) (n-1) + f (m - 1) n
I have already looked at memoization, but haven't been able to implement a solution :\\ 我已经研究过记忆,但是还无法实现解决方案:\\
Suggestions? 建议? :)
:)
A great explanation is here . 这里有一个很好的解释。
I love memoize package :) 我喜欢备忘录包:)
Example (solving the "A frog is jumping up the staircase..." problem): 示例(解决“青蛙跳上楼梯...”的问题):
import Data.Function.Memoize
ladder :: Integer -> Integer -> Integer
ladder n k = g n
where g = memoize f
f 0 = 1
f x = sum [g (x - y) | y <- [1..if x < k then x else k]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.