试图通过fmap(转换)使wireworld移动,但它以某种方式向我显示错误(Haskell)

时间:2013-04-05 07:46:34

标签: haskell map

和我上一个问题一样,我要求通过有序列表创建一个wireworld,所以我编写了以下代码,(代码中的所有函数都以某种方式在其他模块中定义,所以,不要担心XD ,随便问我是否想看看那些“预定义的功能”)但是当我在终端上运行时,它显示错误,这里是代码:

module Transitions.For_Ordered_Lists_2D (
   transition_world -- :: Ordered_Lists_2D Cell -> Sparse_Line Cell
) where

import Data.Cell (Cell (Head, Tail, Conductor, Empty))
import Data.Coordinates
import Data.Ordered_Lists_2D


-- Replace this function with something more meaningful:


xandy :: Element_w_Coord Cell -> Coord
xandy (e, (x, y)) = (x, y)

transition_sc :: Ordered_Lists_2D Cell -> Placed_Elements Cell -> Sparse_Line Cell
transition_sc world pec = case world of
Sparse_Line{y_pos = y, entries =  xline}: rest_of_sparse_lines  -> case pec of
        Placed_Element{x_pos = x, entry =  Head} : rest_of_placed_elements   -> (Sparse_Line{y_pos = y, entries = Placed_Element{x_pos = x, entry =  Tail} :  rest_of_placed_elements}) 
        Placed_Element{x_pos = x, entry =  Tail} : rest_of_placed_elements   -> (Sparse_Line{y_pos = y, entries = Placed_Element{x_pos = x, entry =  Conductor} : rest_of_placed_elements}) 
        Placed_Element{x_pos = x, entry =  Empty} : rest_of_placed_elements   -> (Sparse_Line{y_pos = y, entries = Placed_Element{x_pos = x, entry =  Empty} : rest_of_placed_elements}) 
        Placed_Element{x_pos = x, entry =  Conductor} : rest_of_placed_elements
            |element_occurrence Head neighbours == 1 || element_occurrence Head neighbours == 2    -> (Sparse_Line{y_pos = y, entries = Placed_Element{x_pos = x, entry =  Head} : rest_of_placed_elements}) 
            |otherwise                                                                             -> (Sparse_Line{y_pos = y, entries = Placed_Element{x_pos = x, entry =  Conductor} : rest_of_placed_elements}) 
                where
                    neighbours = local_elements (xandy (Conductor, (x, y))) world


transition_world :: Ordered_Lists_2D Cell -> Ordered_Lists_2D Cell
transition_world world = fmap (transition_sc world) world      

--the end
--the end
--the end

然而它显示以下错误:

u5363876@n114lt20:~/Desktop/lalal$ ./make_Wireworld
[10 of 20] Compiling Transitions.For_Ordered_Lists_2D ( Sources/Transitions/For_Ordered_Lists_2D.hs, x86_64/Transitions/For_Ordered_Lists_2D.o )

Sources/Transitions/For_Ordered_Lists_2D.hs:35:53:
    Couldn't match expected type `Placed_Elements Cell'
                with actual type `Sparse_Line Cell'
    Expected type: [Placed_Elements Cell]
      Actual type: Ordered_Lists_2D Cell
    In the second argument of `fmap', namely `world'
    In the expression: fmap (transition_sc world) world

我对这个错误完全感到困惑,请提前感谢任何可以帮助我的人。

@ dave4420这里是Placed_Elements和Sparse_Lines

的定义
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]

2 个答案:

答案 0 :(得分:1)

你有:

transition_sc :: Ordered_Lists_2D Cell -> Placed_Elements Cell -> Sparse_Line Cell

它期望它的第二个参数为[Placed_Element Cell] - 即Placed_Element列表。但是你要映射它,所以一次只能传递一个参数而不是整个列表。此外,您在Sparse_Line Cell

列表中调用它

实际上,每个Sparse_Line都有一个Placed_Element的列表,因此可以使这项工作成功。但是你需要“深入”到Placed_Elements的每个元素才能到达那里。

尝试编写transition_sc类型的Ordered_Lists_2D Cell -> Sparse_Line Cell -> Sparse_Line Cell函数,看看是否有助于您进一步发展。

答案 1 :(得分:1)

你有非常奇怪的案例

不要使用

case pec of
    Placed_Element{x_pos = x, entry =  Head} : rest_of_placed_elements   -> ...

改为使用

case entry (head pec) of
    Head   -> ...