Haskell 中的部分记忆

发布于 2024-12-04 08:56:44 字数 696 浏览 1 评论 0原文

我试图找到一种好方法,使用 Data.MemoCombinators 来记住 Haskell 中函数的部分域(非负整数)。

import Data.MemoCombinators

--approach 1

partFib n | n < 0 = undefined
          | otherwise = integral fib n where
  fib 0 = 1
  fib 1 = 1
  fib k = partFib (k-1) + partFib (k-2)

--approach 2

partFib2 n | n < 0 = undefined
           | otherwise = fib n
fib = integral fib'
  where
    fib' 0 = 1
    fib' 1 = 1
    fib' n = partFib2 (n-1) + partFib2 (n-2)

方法1是我想要的方法,但是它似乎不起作用。我认为这是因为每次调用 partFib 时都会“重新创建”fib 函数,从而丢弃了记忆。 fib 不依赖于 partFib 的输入,因此您会假设编译器可以提升它,但显然 GHC 不能那样工作。

方法2是我最终的做法。哎呀,很多丑陋的接线。

有谁知道更好的方法来做到这一点?

I'm trying to find a good way to memoize a function for only part of its domain (non-negative integers) in Haskell, using Data.MemoCombinators.

import Data.MemoCombinators

--approach 1

partFib n | n < 0 = undefined
          | otherwise = integral fib n where
  fib 0 = 1
  fib 1 = 1
  fib k = partFib (k-1) + partFib (k-2)

--approach 2

partFib2 n | n < 0 = undefined
           | otherwise = fib n
fib = integral fib'
  where
    fib' 0 = 1
    fib' 1 = 1
    fib' n = partFib2 (n-1) + partFib2 (n-2)

Approach 1 is how I would like to do it, however, it doesn't seem to work. I assume this is because the fib function is "recreated" every time partFib is called, throwing away the memoization. fib doesn't depend on the input of partFib, so you would assume that the compiler could hoist it, but apparently GHC doesn't work that way.

Approach 2 is how I end up doing it. Eerk, a lot of ugly wiring.

Does anybody know of a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

很酷又爱笑 2024-12-11 08:56:45

不太确定什么对你来说“丑陋”,但是通过将记忆操作从 n 的函数中提升出来,你可以在仅使用单个顶级标识符的情况下进行适当的记忆。

partFib3 = \n -> if n < 0 then undefined else fib' n
    where fib 0 = 1
          fib 1 = 1
          fib k = partFib3 (k-1) + partFib3 (k-2)
          fib' = integral fib

Not quite sure what's "ugly" to your eye, but you can have proper memoization while using only a single top-level identifier by lifting the memoization operation out of the function of n.

partFib3 = \n -> if n < 0 then undefined else fib' n
    where fib 0 = 1
          fib 1 = 1
          fib k = partFib3 (k-1) + partFib3 (k-2)
          fib' = integral fib
著墨染雨君画夕 2024-12-11 08:56:45

嗯,稍微分开一下怎么样:

fib 0 = 0
fib 1 = 1
fib x = doFib (x-1) + doFib (x-2)

memFib = Memo.integral fib

doFib n | n < 0 = fib n
        | otherwise memFib n

现在您需要使用 doFib。

Hmm what about separating things a bit:

fib 0 = 0
fib 1 = 1
fib x = doFib (x-1) + doFib (x-2)

memFib = Memo.integral fib

doFib n | n < 0 = fib n
        | otherwise memFib n

Now you need to use doFib.

北笙凉宸 2024-12-11 08:56:45

库中有一个用于此目的的组合器:

switch :: (a -> Bool) ->备注一->备注一->备忘录一  

switch pa b 每当 p 给出 true 时使用备忘录表 a ,每当 p 给出 true 时使用备忘录表 b code>p 给出 false。

回想一下,id 从技术上讲是一个记忆器(它不会记忆:-),所以你可以这样做:

partFib = Memo.switch (< 0) id Memo.integral fib'
    where
    ...

There is a combinator in the library for this purpose:

switch :: (a -> Bool) -> Memo a -> Memo a -> Memo a  

switch p a b uses the memo table a whenever p gives true and the memo table b whenever p gives false.

Recall that id is technically a memoizer (which does not memoize :-), so you can do:

partFib = Memo.switch (< 0) id Memo.integral fib'
    where
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文