Error in protection state when combining list and single item

I am trying to add the list header ( r) to the list sack, however I keep getting this error message.

ERROR "Knapsack.hs":35 - Type error in guarded expression
*** Term           : findItems rt (r : sack) (getTotalWeight sack r)
*** Type           : [Item]
*** Does not match : [[Item]]

Below is the code.

findItems :: [Item] -> [Item] -> Float -> [Item]
findItems (r:rt) sack total 
            | total > 20 = [sack]
            | canContinue = findItems rt (r : sack ) (getTotalWeight sack r) 
            | otherwise = [sack] 
            where canContinue = (getTotalWeight sack r) < 20 
+3
source share
2 answers

You cannot return [sack]from findItems, because it sackalready has a type [Item], therefore it [sack]has a type [[Item]]. Remove the brackets.

, ( [Item], ), ( ), Haskell , , , re , , [[Item]]. , | findItems. , , :)

+6

, , "Hugs-ism", Hugs, , -, GHC. GHCi, , , :

| total > 20 = [sack]

GHCi ( IMHO, , , !).

+3

All Articles