Parsing 在Haskell中编译时出现分析错误(x+;1)
我正在做这个任务,我非常想让它发挥作用。 我知道这不是最聪明的方法,也不是最有效的方法。我这样做纯粹是因为我想测试这段代码有多低效Parsing 在Haskell中编译时出现分析错误(x+;1),parsing,haskell,compilation,Parsing,Haskell,Compilation,我正在做这个任务,我非常想让它发挥作用。 我知道这不是最聪明的方法,也不是最有效的方法。我这样做纯粹是因为我想测试这段代码有多低效 transition_from_conductor :: Element_w_Coord Cell -> List_2D Cell -> Cell transition_from_conductor element world = case num_of_heads_around_conductor (0, element) world of 1
transition_from_conductor :: Element_w_Coord Cell -> List_2D Cell -> Cell
transition_from_conductor element world = case num_of_heads_around_conductor (0, element) world of
1 -> Head
2 -> Head
_ -> Conductor
where
num_of_heads_around_conductor :: (Int, Element_w_Coord Cell) -> List_2D Cell -> Int
num_of_heads_around_conductor (i, (cell, (x, y))) ((w_cell, (w_x, w_y): rest)) = case rest of
[] -> i
_ -> case (w_cell, w_x, w_y) of
(Head, (x + 1), y) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
(Head, (x + 1), (y + 1)) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
(Head, (x + 1), (y - 1)) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
(Head, (x - 1), y) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
(Head, (x - 1), (y + 1)) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
(Head, (x - 1), (y - 1)) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
(Head, x, (y + 1)) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
(Head, x, (y - 1)) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
_ -> num_of_heads_around_conductor ( i , (cell, (x, y))) (rest)
如果我试着在终端中运行它,它会在终端上给出解析错误(x+1)
我做错了什么?我该如何修复它
有几件事
type List_2D e = [Element_w_Coord e]
type Element_w_Coord e = (e, Coord)
type Coord = (X_Coord, Y_Coord)
type X_Coord = Integer
type Y_Coord = Integer
谢谢大家:D您使用的是“n+k”模式,它已从语言中删除。不能再使用“+”对整数进行模式匹配。在大多数情况下,模式匹配仅限于构造函数和文本
为了获得与模式匹配相同的结果,我建议:
(Head, x0, y) -> let x = x0 - 1 in ...
请注意,这段代码还有更多错误-模式匹配是重叠的。例如:即使有n+k支持,也没有模式:
(Head, (x + 1), y)
失败,下一个模式为:
(Head, (x + 1), (y + 1))
成功。换句话说,您有许多案例永远无法执行。您使用的是“n+k”模式,它已从语言中删除。不能再使用“+”对整数进行模式匹配。在大多数情况下,模式匹配仅限于构造函数和文本
为了获得与模式匹配相同的结果,我建议:
(Head, x0, y) -> let x = x0 - 1 in ...
请注意,这段代码还有更多错误-模式匹配是重叠的。例如:即使有n+k支持,也没有模式:
(Head, (x + 1), y)
失败,下一个模式为:
(Head, (x + 1), (y + 1))
成功。换句话说,你有许多案例永远无法执行。这里有两件事错了:
- 不能在模式中使用算术表达式
- 当您尝试对
和x
进行模式匹配时,您打算将模式的这些部分约束为等于现有的y
和x
变量,但您却创建了新的变量y
和x
y
_ -> case (w_cell, w_x, w_y) of
(Head, x', y')
| x' == x + 1 && y' == y -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
| x' == x + 1 && y' == y + 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
| x' == x + 1 && y' == y - 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
| x' == x - 1 && y' == y -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
| x' == x - 1 && y' == y + 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
| x' == x - 1 && y' == y - 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
| x' == x && y' == y + 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
| x' == x && y' == y - 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
_ -> num_of_heads_around_conductor ( i , (cell, (x, y))) (rest)
然后简化:
_ -> case (w_cell, w_x, w_y) of
(Head, x', y')
| x' == x + 1 && (y' == y || y' == y + 1 || y' == y - 1)
|| x' == x - 1 && (y' == y || y' == y + 1 || y' == y - 1)
|| x' == x && (y' == y + 1 || y' == y - 1)
-> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
_ -> num_of_heads_around_conductor ( i , (cell, (x, y))) (rest)
毫无疑问,这可以进一步简化。这里有两个错误:
- 不能在模式中使用算术表达式
- 当您尝试对
和x
进行模式匹配时,您打算将模式的这些部分约束为等于现有的y
和x
变量,但您却创建了新的变量y
和x
y
_ -> case (w_cell, w_x, w_y) of
(Head, x', y')
| x' == x + 1 && y' == y -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
| x' == x + 1 && y' == y + 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
| x' == x + 1 && y' == y - 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
| x' == x - 1 && y' == y -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
| x' == x - 1 && y' == y + 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
| x' == x - 1 && y' == y - 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
| x' == x && y' == y + 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
| x' == x && y' == y - 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
_ -> num_of_heads_around_conductor ( i , (cell, (x, y))) (rest)
然后简化:
_ -> case (w_cell, w_x, w_y) of
(Head, x', y')
| x' == x + 1 && (y' == y || y' == y + 1 || y' == y - 1)
|| x' == x - 1 && (y' == y || y' == y + 1 || y' == y - 1)
|| x' == x && (y' == y + 1 || y' == y - 1)
-> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest)
_ -> num_of_heads_around_conductor ( i , (cell, (x, y))) (rest)
毫无疑问,这可以进一步简化。可以使用视图模式:(Head,subtract 1->x,y)似乎其目的是与外部范围中的
x
和y
的值相匹配,而不是n+k模式。参见dave4420的答案。可以使用视图模式:(Head,subtract 1->x,y)看起来它的意图是与外部范围中的x
和y
的值相匹配,而不是n+k模式。参见dave4420的答案。我们是否知道abs(x-x')<2和&abs(y-y')<2
?我没有仔细阅读OP的代码来判断。它看起来像是在试图匹配网格中一个单元格周围的8个邻居,因此,加上一个精确匹配的例外就足够了。我们知道abs(x-x')<2&&abs(y-y')<2
?我没有仔细阅读OP的代码来判断。它看起来像是在试图匹配网格中一个单元格周围的8个邻居,因此,加上一个精确匹配的例外,应该就足够了。