【发布时间】:2014-02-19 15:59:12
【问题描述】:
这是一个简单的 Haskell 记忆函数 f1 接受一个参数(是的,斐波那契):
f1 = [calc n | n <- [0..]]
where calc 0 = 0
calc 1 = 1
calc n = f1 !! (n-1) + f1 !! (n-2)
现在,对于接受 2 个参数的函数 f2 或接受 3 个参数的函数 f3,如何做到这一点?
对于 f2,最好的方法是列表吗?或者可以使用不同的数据结构吗?
f2 = [[calc n m | n <- [0..]] | m <- [0..]]
where calc 0 0 = 0
calc a b = // ...something using (f2 !! a !! b)
对于f3 a b c,鉴于max_a * max_b * max_c 是可管理的,这种记忆/动态编程将如何工作?
我正在寻找最简单/最直接的方法,尽可能使用标准 Haskell 库。
编辑
正如 Chris Taylor 的回答所建议的那样,我尝试使用 MemoCombinators.hs v0.5.1 但它对我来说失败了,如下所示:
Could not find module `Data.IntTrie'
Use -v to see a list of the files searched for.
和
Illegal symbol '.' in type
Perhaps you intended -XRankNTypes or similar flag
to enable explicit-forall syntax: forall <tvs>. <type>
我需要它在“普通”haskell 中运行,这个版本:GHCi, version 7.6.3
有什么小窍门吗?
【问题讨论】:
标签: haskell dynamic-programming memoization