我正在尝试对列表进行排序,并检查我的排序算法是否正常工作,因为我希望我打印出排序列表中的特定元素,我希望这是一个简单的任务,但事实证明非常困难 - 我想我会以错误的方式去做.
data Candidate = Candidate Float Float Float String
Run Code Online (Sandbox Code Playgroud)
...
getName :: Candidate -> String
getName (Candidate weight profit effic name) = name
Run Code Online (Sandbox Code Playgroud)
...
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))
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
Couldn't match expected type `[b0]' with actual …
Run Code Online (Sandbox Code Playgroud) 我正进入(状态
TypeError: 'NoneType' object is not iterable
Run Code Online (Sandbox Code Playgroud)
当我试图找到一个列表的总和.
出现问题的地方:
if(sum(self._candidates) + self._allCandidates[self._depth]._weight > 20):
self._left = Node(self._candidates, self._depth + 1, self._allCandidates)
else:
self._left = Node(self._candidates.append(self._allCandidates[self._depth]), self._depth + 1, self._allCandidates)
Run Code Online (Sandbox Code Playgroud)
节点定义:
def __init__(self, candidates = [], depth = -1, allCandidates = []):
self._candidates = candidates
self._depth = depth
self._allCandidates = allCandidates
Run Code Online (Sandbox Code Playgroud)
感谢您对此事的任何帮助.