0

我正在做这项任务,我迫切希望让它发挥作用。我知道这不是最聪明的方法,也不是最有效的方法。我这样做纯粹是因为我想测试这段代码的效率有多低。

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)

(Head, (x + 1), y) .....

我做错了什么?我该如何解决?

一些东西...

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

4

2 回答 2

3

您使用的是“n + k”模式,该模式已从语言中删除。您不能再使用“+”对整数进行模式匹配。在大多数情况下,模式匹配仅限于构造函数和文字。

为了达到与模式匹配相同的结果,我建议:

(Head, x0,  y)        -> let x = x0 - 1 in ...

请注意,此代码还有更多错误 - 模式匹配是重叠的。例如:即使有 n+k 支持,也不会出现以下模式:

(Head, (x + 1),  y)

失败和下一个模式:

(Head, (x + 1), (y + 1))

成功。换句话说,你有许多永远无法执行的案例。

于 2013-03-20T13:04:55.997 回答
3

这里有两点错误:

  • 您不能在模式中使用算术表达式
  • 当您尝试进行模式匹配x并且y您打算将模式的这些部分限制为等于现有的xy变量时,而是创建新的变量xy

我会使用警卫。

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

毫无疑问,这可以进一步简化。

于 2013-03-20T15:19:47.450 回答