4

I am strugling with the following problem. I am trying to make Game of Life using store comonads in Haskell. I have the following relevant code:

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

I have then created an initial field:

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

The initial field is a small example where I want to test my program on. Now here comes the tricky part.

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)

Now my question is, what is a good way to change the focus onto the updated store? Moving down is simple:

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

My question is, what would be a good way to move right or left for example? Did I chose a bad index function (head.head)?

Thank you for your time!

Benjamin Hodgson
  • 42,952
  • 15
  • 108
  • 157
Thobro
  • 41
  • 2
  • Is there a reason why you use `Float`s here, and not `Int`s? I agree that this is an implementation *detail*, but usually calculations are safer in integer land than in floating point land. – Willem Van Onsem Apr 18 '18 at 17:53
  • Yes later on in the program I visualize my grid using Gloss, and that requires `Float`s as positions. I agree with you and I was using `Int` before Gloss. – Thobro Apr 18 '18 at 17:58
  • 2
    @Thobro There's no reason to use `Float` for the _logical_ model just because the _visual_ model uses it. You can always convert to `Float` from `Int` using `fromIntegral` later on. – Cubic Apr 18 '18 at 18:13
  • Ah yes that is a smart idea :) – Thobro Apr 18 '18 at 18:19
  • Why wouldn't a combination of `pos` and `seek` work to move in any direction for you? – 9000 Apr 18 '18 at 19:55

0 Answers0