2011-08-29 41 views
9

我是一名初學者,我正在嘗試在輸入uni之前做一些關於Haskell的教程計算機科學。無法匹配預期類型(Int - > Int - > Int),其實際類型爲'(t0,t1,t2)'

我被卡在這個程序中。它需要三個數字並按升序排列。任何人都可以幫助我,告訴我什麼是錯的,因爲它讓我瘋狂?謝謝你的時間。

import Prelude hiding (min,max) 
orderTriple :: (Int -> Int -> Int) -> (Int -> Int -> Int) 
max :: Int -> Int -> Int -> Int 
min :: Int -> Int -> Int -> Int 
middle :: Int -> Int -> Int -> Int 


max x y z 
|(x>=y) && (x>=z) = x 
|(y>=x) && (y>=z) = y 
|otherwise  = z 

min d e f 
|(d<=e) && (d<=f) = d 
|(e<=d) && (e<=f) = e 
|otherwise  = f 

middle g h i 
| (g <= (max g h i)) && (g >= (min g h i)) = g 
| (h <= (max g h i)) && (h >= (min g h i)) = h 
| otherwise     = i 

orderTriple (a,b,c) = ((min a b c),(middle a b c),(max a b c)) 

的錯誤是:

orderList.hs:23:13: 
    Couldn't match expected type `[Int -> Int -> Int]' 
       with actual type `(t0, t1, t2)' 
    In the pattern: (a, b, c) 
In an equation for `orderTriple': 
    orderTriple (a, b, c) = [(min a b c), (middle a b c), (max a b c)] 
+0

你居然要在大學裏做Haskell(或某種FP)?我希望我已經在我的程序中獲得了這一點! – MatrixFrog

回答

6

你給編譯器錯誤類型的信息:

orderTriple :: (Int -> Int -> Int) -> (Int -> Int -> Int) 

應該

orderTriple :: (Int, Int, Int) -> (Int, Int, Int) 

第一打字聲稱orderTriple轉換是一個函數(從兩個Ints到另一個)到另一個這樣的函數中,這根本不是你的代碼所做的。 (另外:用於研究FP 的準備爲CS程序)。

+0

thx很多關於你的幫助,但這次它出現了另一個錯誤 :1:1: 函數'orderTriple'適用於三個參數, 但它的類型'(Int,Int,Int) - > Int,Int,Int)'只有一個 在表達式中:orderTriple 3 1 9 在'it'的等式中:it = orderTriple 3 1 9 – noobie

+0

'orderTriple'的定義需要一個元組,即逗號 - 用括號分隔參數。你必須這樣調用它 - 'orderTriples(3,1,9)' - 不要使用三個空格分隔的(curried)參數,而不要使用括號。 –

+0

@noobie:你可能還想在'orderTriple'的定義中檢查你使用的是圓括號'()'而不是方括號'[]'。你給的錯誤信息是不同於上面的代碼清單... – yatima2975

2

箭頭->分隔函數的參數。 (其實這是多一點點複雜),但對一個元組的參數分開使用逗號:

orderTriple :: (Int,Int,Int) -> (Int,Int,Int) 

在其他類型的情況下,空間足夠:

Either String Int 
相關問題