gpt4 book ai didi

haskell - 使用 netwire 的周期性与 at 电线

转载 作者:行者123 更新时间:2023-12-02 03:35:28 25 4
gpt4 key购买 nike

我正在尝试用 Haskell 编写实时交互式图形框架。我一直试图通过使用 Netwire 5 来处理事情,但我似乎没有很好地处理事情如何“依赖”彼此。 For example , 下面的代码应该在切换到 (val + 1) 之前产生 val 两秒钟,然后无限期地继续下去。

someWire :: Num a => a -> Wire s e m a a
someWire x = (-->) ((pure x) &&& (periodic 2) >>> until) (someWire (x + 1))

但是,这会造成某种内存泄漏,我的程序会在这种情况下停止并一直分配内存,直到我的系统崩溃。或者,这个定义

someWire :: Num a => a -> Wire s e m a a
someWire x = (-->) ((pure x) &&& (at 2) >>> until) (someWire (x + 1))

表现出我期望的方式:从 val 开始计数,每两秒更改一次值。有人可以解释这种行为吗?

最佳答案

关键是periodic立即产生一个事件。

因此,当我们要从这根电线中产生一个值时,我们必须将其评估为以下内容:

someWire x
(-->) ((pure x) &&& (periodic 2) >>> until) (someWire (x + 1))
(-->) (pure (x, Event _) >>> until) (someWire (x + 1))
(-->) *inhibition* (someWire (x + 1))
someWire (x + 1)

因为这不是尾递归,垃圾收集器不允许清理之前分配给线路的实例,我们会耗尽内存(而不是陷入无限循环)。

关于haskell - 使用 netwire 的周期性与 at 电线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23714212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com