列出网格元素

发布于 2024-11-07 08:20:12 字数 1406 浏览 6 评论 0原文

我已经成功地在我的作业的这一部分中取得了一些进展,但附上了我所做的下面的部分代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏别ゝ 2024-11-14 08:20:12
update :: Grid -> Point -> Grid
update [[]] (x,y)       =   [[]]

这会检查包含空列表的列表。

update [(g:gs)] (x,y)   =   [(updateRow (g:gs) x)]

这会检查包含一个列表的列表,后者至少包含一个元素(绑定到变量 g)。

您想要检查包含多个列表的列表。

该模式应如下所示:

update :: Grid -> Point -> Grid
update [[]] (x, y)       = [[]]
update (row:rows) (x, 0) = updateRow row x : rows
update (row:rows) (x,y)  = -- I'll let you fill this, notice the 0 on the previous line

记住 Grid 只是 Row 的列表。

第二行现在的意思是“如果你想更新这个网格的第 0 行,那么更新第一行”,最后一行应该意味着“如果你想更新这个网格的第 y 行,那么保留第一行不变” ,并递归更新其余行”(当然,必须在递归调用中相应地更改 y)。

update :: Grid -> Point -> Grid
update [[]] (x,y)       =   [[]]

This checks for a list that contains the empty list.

update [(g:gs)] (x,y)   =   [(updateRow (g:gs) x)]

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:

update :: Grid -> Point -> Grid
update [[]] (x, y)       = [[]]
update (row:rows) (x, 0) = updateRow row x : rows
update (row:rows) (x,y)  = -- I'll let you fill this, notice the 0 on the previous line

Remember a Grid is just a list of Rows.

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).

风启觞 2024-11-14 08:20:12

这是解决方案。经过一番思考,我得出以下结论并填写了上述“模式”的最后一行:

...
update        (g:gs)  (x,y) =  g : update gs (x,(y-1))

Here is the solution. After some thought, I came out with the following and filled the final line of the above 'pattern':

...
update        (g:gs)  (x,y) =  g : update gs (x,(y-1))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文