1
我想一個列表排序,並檢查我的排序算法正在工作,我希望我想打印出排序列表中的具體內容,這是我所期望的是從列表中打印的元素一個簡單的任務,但被證明是非常困難的 - 我想我會以錯誤的方式去做。在Haskell
data Candidate = Candidate Float Float Float String
...
getName :: Candidate -> String
getName (Candidate weight profit effic name) = name
...
main = do
let items = [Candidate 0.20 4.17 (calculateEfficiency 0.20 4.17) "Weapon"]
Candidate 3.11 4.53 (calculateEfficiency 3.11 4.53) "Tinned food":items
Candidate 1.04 4.64 (calculateEfficiency 1.04 4.64) "Ammunition":items
Candidate 2.70 1.19 (calculateEfficiency 2.70 1.19) "Water":items
let sortedItems = sortBy mySort items
putStrLn (getName (sortedItems !! 0))
我得到的錯誤是:
Couldn't match expected type `[b0]' with actual type `IO()'
In the return type of a call of `putStrLn'
In the expression: putStrLn (getName (sortedItems !! 0))
In the expression:
do { let items = ...;
Candidate 3.11 4.53 (calculateEfficiency 3.11 4.53) "Tinned food"
: items;
Candidate 1.04 4.64 (calculateEfficiency 1.04 4.64) "Ammunition"
: items;
Candidate 2.7 1.19 (calculateEfficiency 2.7 1.19) "Water" : items;
.... }
Failed, modules loaded: none.
感謝您的幫助。
你認爲那些'Canidate ...:items'行可以做什麼? – delnan 2012-03-28 13:30:56
將候選人添加到列表中 – rhalliwell1 2012-03-28 13:35:33