【发布时间】:2012-03-28 13:26:19
【问题描述】:
我正在尝试对列表进行排序并检查我的排序算法是否正常工作我想打印出排序列表的特定元素,我希望这是一项简单的任务,但事实证明是非常困难——我想我做错了。
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行做什么?
标签: list sorting haskell printing ghci