列出网格元素
我已经成功地在我的作业的这一部分中取得了一些进展,但附上了我所做的下面的部分代码:
module Grid where
data State = On | Off deriving (Eq, Show)
next :: State -> State
next On = Off
next Off = On
type Row = [State]
type Grid = [[State]]
type Point = (Int,Int)
initialRow :: Int -> Row
initialRow w = replicate w Off
updateRow :: Row -> Int -> Row
updateRow (r:rs) x
| x==0 = next r:rs
| otherwise = r : (updateRow rs (x-1))
update :: Grid -> Point -> Grid
update [[]] (x,y) = [[]]
update [(g:gs)] (x,y) = [(updateRow (g:gs) x)]
如上面最后一行所示,我已经设法让更新在 x = any Int as 时工作如下所示(第 x 个元素反转)- ghci。
*Grid> update [[Off,Off,Off,Off]] (2,0)
[[Off,Off,On,Off]]
*Grid>
然而,当我尝试使用这样的多个列表,或者选择列表中的某个列表来更新第 x 个元素时,这一切都变得不稳定:
*Grid> update [[Off,Off,Off,Off],[Off,Off,Off,Off]] (2,0)
*** Exception: Grid.hs:(24,0)-(25,47): Non-exhaustive patterns in function update
我似乎无法“概括”此函数中的公式。
我还必须遵循这种类型约定:
updateRow :: Grid -> Point -> Grid
基本上,我想做的就是从这样的内容更新...
[[Off,Off,Off,Off],
[Off,Off,Off,Off],
[Off,Off,Off,Off],
[Off,Off Off,Off]]
到这样的:
[[Off,Off,Off,Off],
[Off,Off,**On**,Off],
[Off,Off,Off,Off],
[Off,Off Off,Off]]
其中“x”是元素的值,“y”是列表中的值列出 IYGWIM。
提前致谢。
I have managed to make some progress in this part of my assignment but have attached part of the code below that I have made:
module Grid where
data State = On | Off deriving (Eq, Show)
next :: State -> State
next On = Off
next Off = On
type Row = [State]
type Grid = [[State]]
type Point = (Int,Int)
initialRow :: Int -> Row
initialRow w = replicate w Off
updateRow :: Row -> Int -> Row
updateRow (r:rs) x
| x==0 = next r:rs
| otherwise = r : (updateRow rs (x-1))
update :: Grid -> Point -> Grid
update [[]] (x,y) = [[]]
update [(g:gs)] (x,y) = [(updateRow (g:gs) x)]
As shown in the last line just above, I have managed to get update to work for when x = any Int as shown below (with the xth element inverted) - ghci.
*Grid> update [[Off,Off,Off,Off]] (2,0)
[[Off,Off,On,Off]]
*Grid>
It all comes unstuck however when I try working with multiple lists such as this, or select a certain list within the list to update the xth element:
*Grid> update [[Off,Off,Off,Off],[Off,Off,Off,Off]] (2,0)
*** Exception: Grid.hs:(24,0)-(25,47): Non-exhaustive patterns in function update
I can't seem to 'genralise' a formula in this function.
I also MUST follow THIS type convention:
updateRow :: Grid -> Point -> Grid
Basically, what I would like to do is update from something like this...
[[Off,Off,Off,Off],
[Off,Off,Off,Off],
[Off,Off,Off,Off],
[Off,Off Off,Off]]
to this:
[[Off,Off,Off,Off],
[Off,Off,**On**,Off],
[Off,Off,Off,Off],
[Off,Off Off,Off]]
where 'x' is the value of the element and 'y' is the value of the list within the list IYGWIM.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这会检查包含空列表的列表。
这会检查包含一个列表的列表,后者至少包含一个元素(绑定到变量 g)。
您想要检查包含多个列表的列表。
该模式应如下所示:
记住
Grid
只是Row
的列表。第二行现在的意思是“如果你想更新这个网格的第 0 行,那么更新第一行”,最后一行应该意味着“如果你想更新这个网格的第 y 行,那么保留第一行不变” ,并递归更新其余行”(当然,必须在递归调用中相应地更改 y)。
This checks for a list that contains the empty list.
This checks for a list that contains one list, the latter containing at least one element (bound to the variable g).
You want to check for a list that contains multiple lists.
The pattern should look like:
Remember a
Grid
is just a list ofRow
s.The second line now means "if you want to update the 0th line of this grid, then update the first row", the last line should mean "if you want to update the yth line of this grid, then leave the first one as is, and recursively update the rest of the rows" (of course, y must be changed accordingly in the recursive call).
这是解决方案。经过一番思考,我得出以下结论并填写了上述“模式”的最后一行:
Here is the solution. After some thought, I came out with the following and filled the final line of the above 'pattern':