[英]What can be the problem with my haskell code and how can I fix it?
deadNeighbors: Give a list of empty cells that have a living cellular neighbor in that generation. deadNeighbors:给出在该代中具有活细胞邻居的空细胞列表。 Make sure that each cell is listed only once!确保每个单元格只列出一次!
Something not work with my deadNeighbors function, but I do not know what is wrong about it.有些东西对我的 deadNeighbors function 不起作用,但我不知道它有什么问题。 Can anybody help me to fix deadNeighbors?有人可以帮我修复 deadNeighbors 吗?
type Coordinate = (Integer, Integer)
type Generation = [Coordinate]
single :: Generation
single = [ (42, 42) ]
row :: Generation
row = [ (10, 1), (10, 2), (10, 3) ]
Code:代码:
neighbors :: Coordinate -> [Coordinate]
neighbors (x,y) = [(x-1,y-1), (x-1,y), (x-1,y+1), (x ,y-1), (x,y+1), (x+1,y-1), (x+1,y), (x+1,y+1)]
alive :: Generation -> Coordinate -> Bool
alive x y = elem y x
livingNeighbors :: Generation -> Coordinate -> Int
livingNeighbors a = length .filter (alive a) . neighbors
staysAlive :: Generation -> Coordinate -> Bool
staysAlive a b
| alive a b = livingNeighbors a b `elem` [2,3]
| otherwise = livingNeighbors a b `elem` [3]
Problems:问题:
deadNeighbors :: Generation -> [Coordinate]
deadNeighbors (neighbors (x,y))
|(alive (x,y)) = Nothing
|otherwise = [] ++ (x,y)
Examples:例子:
sort (deadNeighbors single) == sort [(41.41), (41.42), (41.43), (42.41), (42.43), (43.41), (43.42) , (43.43)]
sort (deadNeighbors row) == sort [(9.0), (9.1), (9.2), (10.0), (11.0), (11.1), (11.2) , (9.3), (11.3), (9.4), (10.4), (11.4)]
Let's go over deadNeighbors
function line by line:让我们逐行通过deadNeighbors
go function:
(the type signature looks fine) (类型签名看起来不错)
deadNeighbors (neighbors (x,y))
This is a clause of the deadNeighbors
function and seems to use pattern matching notation, however you use the function neighbors
in the pattern match.这是deadNeighbors
function 的子句,似乎使用模式匹配符号,但是您在模式匹配中使用 function neighbors
。 That is not allowed.那是不允许的。 I don't really know what your intention is here, so I cannot suggest a way to fix this.我真的不知道你的意图是什么,所以我不能建议解决这个问题的方法。
|(alive (x,y)) = Nothing
Here you are using a guard correctly, but the alive
function requires the generation in addition to the coordinate.在这里你正确地使用了一个守卫,但是alive
的 function 除了坐标之外还需要生成。 You should pass both as arguments here.您应该在此处将两者作为 arguments 传递。
Also, you are returning Nothing
which has type Maybe a
for some a
.此外,您将返回Nothing
类型为Maybe a
的某些a
。 But the signature of the deadNeighbors
function indicates it should return a [Coordinate]
.但是deadNeighbors
function 的签名表明它应该返回一个[Coordinate]
。 Perhaps you intended to write []
(the empty list)?也许你打算写[]
(空列表)?
|otherwise = [] ++ (x,y)
Here you are using the ++
operator, which expects two lists as arguments, with an empty list []
as argument and a coordinate (x,y)
.这里您使用了++
运算符,它需要两个列表作为 arguments,一个空列表[]
作为参数和一个坐标(x,y)
。 The coordinate type is not a list, so it is not compatible with the ++
operator.坐标类型不是列表,因此与++
运算符不兼容。 Perhaps you intended (x,y): []
or just [(x,y)]
(which means the same thing but is slightly prettier)?也许您打算(x,y): []
或只是[(x,y)]
(意思相同但稍微漂亮一点)?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.