2017-11-17 68 views
3

我正在爲類寫一個多米諾骨牌遊戲,無法將自己的頭包裹在自定義類型中。我有:從Haskell類型同義詞中提取值

type DomsPlayer = Hand -> Board -> (Domino,End) 
... 
playTurn :: DomsPlayer -> (Int, Hand, Board) 
playTurn hand1 board1 = (score, hand2, board2) 
    where (dom, end) = simplePlayer hand1 board1 
        board2 = resMaybe (playDom dom board1 end) 
        hand2 = remove dom hand1 
        score = scoreBoard board2 

嘗試加載這給了我的錯誤:

Dominoes.hs:43:3: error:

• Couldn't match expected type ‘(Int, Hand, Board)’ with actual type ‘Board -> (Int, b0, Board)’

• The equation(s) for ‘playTurn’ have two arguments, but its type ‘DomsPlayer -> (Int, Hand, Board)’ has only one

| 43 | playTurn hand1 board1 = (score, hand2, board2) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

Dominoes.hs:44:37: error:

• Couldn't match type ‘Hand -> Board -> (Domino, End)’ with ‘[Domino]’

Expected type: Hand Actual type: DomsPlayer

• Probable cause: ‘hand1’ is applied to too few arguments

In the first argument of ‘simplePlayer’, namely ‘hand1’

In the expression: simplePlayer hand1 board1

In a pattern binding: (dom, end) = simplePlayer hand1 board1

| 44 | where (dom, end) = simplePlayer hand1 board1 |

如何檢索DomsPlayer值是多少?

+0

它看起來像DomsPlayer的定義是一個函數的類型同義詞,而不是數據類型。 – Zpalmtree

+0

您的類型'DomsPlayer'是兩個參數('Hand'和'Board')的函數。 你的函數'playTurn'作爲一個參數,例如'DomsPlayer'函數返回一個三元組。然而,你的'playTurn'模式匹配有2個參數('hand1'和'board1')。這不適合在一起。 – mschmidt

+1

我給了一個答案,但爲了幫助你,我想知道你在做什麼。你想收到一個'''''''和'''Board'''嗎?或者你想要一個'''(Domino,End)'''?顯然你正在定義一個函數,因爲'''DomsPlayer''是函數的同義詞。 –

回答

0

如果與其定義

type DomsPlayer = Hand -> Board -> (Domino,End) 

playTurn :: (Hand -> Board -> (Domino,End)) -> (Int, Hand, Board) 

更換此DomsPlayer你會看到playTurn臨危只有一個參數,而不是兩個。

我不知道你正在嘗試做的,但顯然你需要收到HandBoard作爲單獨的PARAMS:

playTurn :: Hand -> Board -> ... -> (Int, Hand, Board) 

而且我不知道,也許還傳遞的功能鍵入DomsPlayer,並將其應用於這些單獨的參數。

但這是你的錯誤。