如何避免Helm / Elerea(Haskell FRP)中的内存/ CPU使用失控?

时间:2014-07-09 20:14:14

标签: haskell frp elerea

我在FRP中蘸了我的脚趾,我把一个基本的“你好世界”扔到了一起#34;带Helm的样式应用程序(基于示例here):

import FRP.Helm
import qualified FRP.Helm.Window as Window
import qualified FRP.Helm.Keyboard as Keyboard
import FRP.Elerea.Simple

data SpriteState = SpriteState {mx::Double, my::Double}

inputSignal::SignalGen (Signal (Double, Double))
inputSignal = lift toFrac Keyboard.arrows
  where toFrac (dx, dy) = (realToFrac dx, realToFrac dy)

spriteSignal::SignalGen (Signal SpriteState)
spriteSignal = foldp newState initialState inputSignal
  where
    initialState = SpriteState {mx = 0, my = 200}
    newState (dx, dy) state = state {mx = x', my = y'}
      where x' = mx state + dx
            y' = my state + dy

spriteForm::SpriteState -> Form
spriteForm state = move (mx state, my state) img
  where img = filled blue $ square 64

render::SpriteState -> (Int, Int) -> Element
render spriteState (w, h) = centeredCollage w h $ [spriteForm spriteState]

main = do
  engine <- startup defaultConfig
  run engine $ render <~ spriteSignal ~~ (Window.dimensions engine)

该程序可以运行,但即使没有用户输入,cpu和内存使用量也会迅速攀升,直至崩溃。

我怀疑foldp呼叫正在累积状态,永远不会让它消失。我应该使用其他方法来计算当前状态(I don't see a foldp'),还是需要小心强制在某处进行评估,或者完全不需要其他方法?

使用ghc 7.8.2和helm 0.6.0。

0 个答案:

没有答案