Knapsack problem/Continuous: Difference between revisions
→{{header|Haskell}}: hlint, hindent, added output
(→{{header|Haskell}}: hlint, hindent, minor tidying) |
(→{{header|Haskell}}: hlint, hindent, added output) |
||
Line 1,325:
Or similar to above (but more succinct):
<lang haskell>import Data.List (sortBy)
import Data.Ord (comparing)
import Text.Printf (printf)
-- (name, (value, weight))
items =
, ("sausage", (98, 5.9))
]
unitWeight (_, (val, weight)) =
solution k = loop k . sortBy (flip $ comparing unitWeight)
where
| weight < k = putStrLn ("Take all
| otherwise = printf "Take %.2f kg of the %s\n" (k :: Float) name
main = solution 15 items</lang>
{{Out}}
<pre>Take all the salami
Take all the ham
Take all the brawn
Take all the greaves
Take 3.50 kg of the welt</pre>
=={{header|Icon}} and {{header|Unicon}}==
|