2013-04-05 65 views
0

這裏是我的代碼:「無法匹配預期的類型」錯誤

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

回答

1

map_Ordered_Lists_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取一個值,並把它變成一個總是返回一個函數值相同,而不管其說法。)

+0

sry man ...那個地方是爲單元格類型定義的......所以它不工作T_T – libra 2013-04-05 16:08:11

+2

@libra響應是什麼? quetion中的man函數明確地將函數作爲第一個參數('map_Ordered_Lists_2D ::(Element_w_Coord e - > b) - > ...'),並且您明確地將它傳遞給一個構造函數('map_Ordered_Lists_2D Tail'),而您省略代碼,編譯器說這是'Cell'類型。 – 2013-04-05 16:22:17

+2

@libra我把你的確切的代碼,並填補了一些空白 - 'data Cell = Tail |頭| |空的| Conductor',以及一些可扣除的類型同義詞 - 並且它與我提出的改變一起編好。我並不是說這是您的代碼的正確解決方案,但類型應該是正確的。 – 2013-04-05 16:27:39

1

首先,儘量使功能有點小,如:

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 

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 
+0

@ ThomasM.DuBuisson,這些天沒有與Linux的計算機,我的Mac壞了,所以我不能真正測試與,謝謝,但thx和投票無論如何XD – libra 2013-04-12 04:42:06

相關問題