【发布时间】:2016-07-03 05:41:22
【问题描述】:
我想预先计算一些值,然后在需要进行进一步查找时使用这些值。我想出了以下几点:
import qualified Data.Vector.Unboxed as V
lcgen s =
lc
where
lc 0 b = lengths V.! b
lc a b = lengths V.! b - lengths V.! (a - 1) - 1
lengths = V.fromList $ scanl1 ((+) . (1 +)) $ map length $ words s
该函数本质上返回两个单词之间使用的字符数。我使用它如下:
let lc = lcgen "some sentence with a lot of words"
lc 0 0 -- == 4
lc 0 1 -- == 13
在这个实现中,lengths 向量会被记忆吗?此外,我如何知道和/或确认这一点?
【问题讨论】:
-
您可以将trace 添加到
length(length = trace "building..." $ V.fromList ...) 并亲自查看 -
@Carsten 或者,您可以传递
-ddump-simpl选项并查看编译器生成的核心,然后检查那里。 -
@Bakuriu 这可能是 更好 选项是的 - 但你必须了解核心;)
-
@Carsten 谢谢。这就是我需要的。如果您可以发布作为答案,我会接受。
标签: haskell memoization