edit AssocList by key

后端 未结 1 1548
北恋
北恋 2021-01-26 09:35

I\'m trying to build a recursive function that replaces the CellType by the Cell. Just like this:

> editBoard [((2,2),Mine),((2,3),Mine),((3,2),Mine)]((2, 4), F

相关标签:
1条回答
  • 2021-01-26 10:08

    The signature of your function says you are returning a Board, but the otherwise clause will return a list:

    editBoard :: Board -> (Cell, CellType) -> Board
    editBoard (Board ((x, y):xs)) (a, b) 
             | x == a = (Board ((x, b) : xs))
             | otherwise = ((x, y) : editBoard (Board xs) (a, b))

    I think however you make things too complicated. It might be better to make a helper function that works with a list of (Cell, CellType) objects, and returns such list, and let the editBoard wrap and unwrap the content:

    editBoard :: Board -> (Cell, CellType) -> Board
    editBoard (Board bs) xy@(x0, y0) = Board (go bs)
        where go :: [(Cell, CellType)] -> [(Cell, CellType)]
              go … = …

    I leave implementing go as an exercise.

    0 讨论(0)
提交回复
热议问题