3

我正在努力解决以下问题。我正在尝试使用 Haskell 中的商店 comonads 制作生命游戏。我有以下相关代码:

type Cel = ((Float, Float), Bool)
type Field2D = [[Cel]]

然后我创建了一个初始字段:

initialField2D = [[((0.0, 0.0), True), ((0.0, 1.0), True)], 
                  [((1.0, 0.0), True), ((1.0, 1.0), True)]]

初始字段是一个小例子,我想在其中测试我的程序。现在,棘手的部分来了。

initialStore2D :: Store Field Cel
initialStore2D = Store (head.head) initialField2D

f :: (Store Field2D Cel) -> Cel
f (Store f s) = cellUpdate (f s)

cellUpdate :: Cel -> Cel --Simple trivial cell update function that just moves one spot.
cellUpdate ((x, y), a) = ((x+1, y+1), True)

newStore = extend f initialStore2D -- Apply the update function to every cell.
extract newStore --Correctly returns ((1.0, 1.0), True)

现在我的问题是,将焦点转移到更新商店的好方法是什么?向下移动很简单:

goDown :: Store Field Cel
goDown (Store f s) = Store (f.tail) s

我的问题是,例如,向右或向左移动的好方法是什么?我是否选择了错误的索引函数(head.head)?

感谢您的时间!

4

0 回答 0