我正在遞歸的Haskell中創建合併排序函數。有人告訴我,我的評估的一部分,我必須定義類型爲:Haskell - Ord類型是什麼意思?
isort :: Ord a => [a] -> [a]
我假定該函數需要一個數組作爲輸入和輸出數組。我知道ord
是一類。
在上述背景下Ord a
是什麼意思?
這是我的函數:
isort :: Ord a => [a] -> [a]
isort [x] = [x]
isort (x:xs) = insert x (isort xs)
where
insert :: Int -> [Int] -> [Int]
insert a [] = []
insert a (b:c) | a < b = a:b:c
| otherwise = b : insert a c
當我嘗試加載我的函數文件到ghci中我得到的錯誤:
Couldn't match type ‘a’ with ‘Int’
‘a’ is a rigid type variable bound by
the type signature for isort :: Ord a => [a] -> [a]
at LabSheet2.hs:17:10
Expected type: [a]
Actual type: [Int]
...
嘗試在最後三行上重命名'a'? – Jacob
這裏沒有陣列。 '[a]'是一個* list *類型。 – dfeuer