我試圖解決與輸入的分數揹包問題,無法匹配實際類型期望的類型`(T1,T2,T0)」`[A0]`
[("label 1", value, weight), ("label 2", value, weight), ...]
和輸出,
[("label 1", value, solution_weight), ("label 2", value, solution_weight), ...]
,但我不斷收到錯誤computeKnapsack
:
"Couldn't match expected type `(t1, t2, t0)' with actual type `[a0]`"
我無法理解的問題可能是什麼。我試圖創建一個3條目元組列表。我怎樣才能讓它停止期待一個3入口元組?
fst3 (a,b,c) = a
snd3 (a,b,c) = b
trd3 (a,b,c) = c
fst4 (a,b,c,d) = a
snd4 (a,b,c,d) = b
trd4 (a,b,c,d) = c
qud4 (a,b,c,d) = d
sortFrac (a1, b1, c1, d1) (a2, b2, c2, d2)
| d1 > d2 = GT
| d1 <= d2 = LT
fracKnap (x:xs) =
fractionalKnapsack (x:xs) []
fractionalKnapsack (x:xs) fracList =
if length (x:xs) <= 1
then computeKnapsack (sortBy sortFrac (((fst3 x),(snd3 x),(trd3 x),(snd3 x)/(trd3 x)):fracList)) 100
else fractionalKnapsack xs (((fst3 x),(snd3 x),(trd3 x),(snd3 x)/(trd3 x)):fracList)
computeKnapsack (x:xs) weightLeft =
if length (x:xs) <= 1
then (fst4 x, snd4 x, ((floor (weightLeft/(qud4 x)))*(qud4 x)))
else (fst4 x, snd4 x, ((floor (weightLeft/(qud4 x)))*(qud4 x))):(computeKnapsack xs (weightLeft-(floor (weightLeft/(qud4 x))*(qud4 x))))
請不要使用這些醜陋的'fst''snd''trd''qud'函數來重寫它,這真的很難理解正在發生的事情。每個元組條目必須有某種含義,將其描述爲一種記錄類型('data KnapsackItem = KnapsackItem {propA :: Foo,propB :: Bar,propC :: Quu}') – leftaroundabout 2012-08-05 17:00:55
真的很難說什麼4元組應該代表,使其真的很難遵循您的代碼 – 2012-08-05 17:27:57