Haskell &引用;不能';t匹配预期类型“;错误
这是我的密码:Haskell &引用;不能';t匹配预期类型“;错误,haskell,match,Haskell,Match,这是我的密码: xandy :: Element_w_Coord Cell -> Coord xandy (e, (x, y)) = (x, y) transition_world :: Ordered_Lists_2D Cell -> Element_w_Coord Cell -> Ordered_Lists_2D Cell transition_world world (cell, (x, y)) = case (cell, (x, y)) of (Head, (
xandy :: Element_w_Coord Cell -> Coord
xandy (e, (x, y)) = (x, y)
transition_world :: Ordered_Lists_2D Cell -> Element_w_Coord Cell -> Ordered_Lists_2D Cell
transition_world world (cell, (x, y)) = case (cell, (x, y)) of
(Head, (x, y))-> map_Ordered_Lists_2D Tail world
(Tail, (x, y)) -> map_Ordered_Lists_2D Conductor world
(Empty, (x, y)) -> map_Ordered_Lists_2D Empty world
(Conductor, (x, y)) -> map_Ordered_Lists_2D Head world
下面是错误消息:
Sources/Transitions/For_Ordered_Lists_2D.hs:33:43:
Couldn't match expected type `Element_w_Coord e0 -> Cell'
with actual type `Cell'
In the first argument of `map_Ordered_Lists_2D', namely `Tail'
In the expression: map_Ordered_Lists_2D Tail world
In a case alternative:
(Head, (x, y)) -> map_Ordered_Lists_2D Tail world
有人愿意告诉我我的代码有什么问题吗
顺便说一句,这里是对
type Ordered_Lists_2D e = [Sparse_Line e]
data Sparse_Line e = Sparse_Line {y_pos :: Y_Coord, entries :: Placed_Elements e}
data Placed_Element e = Placed_Element {x_pos :: X_Coord, entry :: e}
type Placed_Elements e = [Placed_Element e]
map_Ordered_Lists_2D :: (Element_w_Coord e -> b) -> Ordered_Lists_2D e -> Ordered_Lists_2D b
map_Ordered_Lists_2D f world = case world of
l: ls -> map_line f l: map_Ordered_Lists_2D f ls
[] -> []
where
map_line :: (Element_w_Coord e -> b) -> Sparse_Line e -> Sparse_Line b
map_line f line = Sparse_Line {y_pos = (y_pos line), entries = map_elements f (y_pos line) (entries line)}
where
map_elements :: (Element_w_Coord e -> b) -> Y_Coord -> Placed_Elements e -> Placed_Elements b
map_elements f y elements = case elements of
c: cs -> Placed_Element {x_pos = (x_pos c), entry = f ((entry c), ((x_pos c), y))}: map_elements f y cs
[] -> []
感谢任何能给我一些建议的人XD的
map\u Ordered\u Lists\u 2D
的第一个参数应该是一个函数,但您正在传递它Tail
,它的类型是Cell
以下内容将进行类型检查,并作为起点帮助您:
transition_world world (cell, (x, y)) = case (cell, (x, y)) of
(Head, (x, y))-> map_Ordered_Lists_2D (const Tail) world
(Tail, (x, y)) -> map_Ordered_Lists_2D (const Conductor) world
(Empty, (x, y)) -> map_Ordered_Lists_2D (const Empty) world
(Conductor, (x, y)) -> map_Ordered_Lists_2D (const Head) world
(函数
const
获取一个值,并将其转换为一个始终返回相同值的函数,而不管其参数是什么。)首先,尝试将函数缩小一点,如:
xandy :: Element_w_Coord Cell -> Coord
xandy (_, coord) = coord
transition_world :: Ordered_Lists_2D Cell -> Element_w_Coord Cell -> Ordered_Lists_2D Cell
transition_world world (cell, _) = map_Ordered_Lists_2D (transition_cell cell) world
where
transition_cell :: Cell -> Cell
transition_cell cell
| Head = Tail
| Tail = Conductor
| Empty = Empty
| Conductor = Head
及
斯雷人。。。该位置是为单元格类型定义的。。。所以它不起作用_T@libra答案是什么?quetion中的man函数显然将函数作为第一个参数(
map\u Ordered\u Lists\u 2D::(Element\w\u Coord e->b)->…
)并且您显然是在向它传递一个构造函数(map\u Ordered\u Lists\u 2D Tail
),当您省略代码时,编译器会说它是Cell
@libra类型的,并填补了一些空白-数据单元=尾|头|空|导体
,以及一些可推断类型的同义词-它按照我建议的更改编译得很好。我不是说这是你代码的正确解决方案,但类型应该是正确的。@ThomasM.DuBuisson,这些天没有linux电脑,而且我的Mac电脑坏了,所以我不能用它进行测试,sry,但是thx和投票XD@PeterHall,最近没有linux电脑,我的Mac电脑坏了,所以我不能用它来测试,sry,但是无论如何,thx和投票XD@ThomasM.DuBuisson,最近没有linux电脑,而且我的Mac电脑坏了,所以我不能用它进行测试,sry,但是thx和投票XD
map_Ordered_Lists_2D :: (Element_w_Coord e -> b) -> Ordered_Lists_2D e -> Ordered_Lists_2D b
map_Ordered_Lists_2D f world = map (map_line f) world
where
map_line :: (Element_w_Coord e -> b) -> Sparse_Line e -> Sparse_Line b
map_line f line = Sparse_Line {y_pos = y_pos line, entries = map_elements f (y_pos line) (entries line)}
map_elements :: (Element_w_Coord e -> b) -> Y_Coord -> Placed_Elements e -> Placed_Elements b
map_elements f y elements = let
place_e c = Placed_Element {x_pos = x_pos c, entry = f (entry c, (x_pos c, y))}
in
map place_e elements