0

和我上一个问题一样,我要求通过有序列表制作一个有线世界,所以我编写了以下代码,(代码中的所有功能都在其他模块中以某种方式定义,所以,不用担心 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]
4

2 回答 2

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看看是否可以帮助您走得更远。

于 2013-04-05T14:43:20.117 回答
1

你有很奇怪的情况

不要使用

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

改为使用

case entry (head pec) of
    Head   -> ...
于 2013-04-06T22:29:51.933 回答