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