여기 분수 배낭 문제를 해결하려면 코드입니다 받았다, 망치로 깨다에 입력하스켈, 배낭 예상하지 반환 형식 오류가
[("label 1", value, weight), ("label 2", value, weight), ...]
그리고 출력 형식이어야합니다 형식이어야합니다
[("label 1", value, solution_weight), ("label 2", value, solution_weight), ...]
코드 : 여기
import Data.List {- Need for the sortBy function -}
{- Input "how much can the knapsack hole <- x" "Possible items in sack [(label, value, weight), ...] <- y" -}
{-knap x [([Char], Integer, Integer), ... ] = -}
knap x [] = []
knap x y = if length y == 1
then
if x > last3 (head y)
then y
else [(frst3 (head y), scnd3 (head y), x)]
else
knap2 x y []
{- x is the knap max, y is the sorted frac list, z is the solution list -}
knap2 x y z = if x == 0
then z
else
if thrd4 (head y) > x
then [((frst4 (head y)), (scnd4 (head y)), x)]
else knap2 (x-(thrd4 (head y))) (tail y) (z++[((frst4 (head y)), (scnd4 (head y)), (thrd4 (head y)))])
{- take a list of labels, values, and weights and return list of labels and fractions -}
fraclist :: (Fractional t1) => [(t, t1, t1)] -> [(t, t1, t1, t1)]
fraclist xs = [(x, y, z, y/z) | (x, y, z) <- xs]
{- Sort the list -}
sortList x = sortBy comparator x
where comparator (_,_,_,d) (_,_,_,h) = if d > h then LT else GT
{- helper func to get values from tuples -}
frst3 (a,b,c) = a
scnd3 (a,b,c) = b
last3 (a,b,c) = c
frst4 (a,b,c,d) = a
scnd4 (a,b,c,d) = b
thrd4 (a,b,c,d) = c
last4 (a,b,c,d) = d
내가
무엇입니까 오류입니다Couldn't match expected type `(t1, t0, t2, t3)'
with actual type `(t1, t0, t2)'
Expected type: [(t1, t0, t2, t3)]
Actual type: [(t1, t0, t2)]
In the second argument of `knap2', namely `y'
In the expression: knap2 x y []
나는 내가 할 수있는 것이 무엇인지 잘 모릅니다. 내가 여기 앉아서 한 시간 동안 벽에 내 머리를 쾅 쾅 쾅쾅 찌그러 뜨리기 전에 누군가 분명히 실수를 지적 할 수 있겠는가? 당신은 오류가 knap2 x y []
에 사용되는 인수 y
에 있음을 알 수
:
knap2는 int, 쿼드 목록 및 트립 목록을 취합니다. 나는 그 전화를 어떻게 제공하지 않는지 모르겠다. Knap2를 knap2로 호출하는 것이 맞습니까? –
아 맞아, 당신의'y'는 3 배가되지만, knap2의 두번째 인수로 전달합니다. –
내 맙소사. 나는 모든 시간을 정렬과 frac 기능을 쓰는 데 보냈다. 나는 그들을 사용하는 것을 잊었다. ......... 나는 그것을 사랑한다. –