Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8320521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:40:41+00:00 2026-06-08T22:40:41+00:00

Here is my code to solve the fractional knapsack problem, input to knap should

  • 0

Here is my code to solve the fractional knapsack problem, input to knap should be in the form

[("label 1", value, weight), ("label 2", value, weight), ...]

And output should be in the form

[("label 1", value, solution_weight), ("label 2", value, solution_weight), ...]

Code:

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

Here is the error I am getting

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 []

I am not quite sure what else I can do. before I sit here and bang my head on the wall for an hour maybe someone could point out an obvious mistake if there are any?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:40:43+00:00Added an answer on June 8, 2026 at 10:40 pm

    I can’t tell how the four-tuples in knap2 and the three-tuples in knap are supposed to fit together but you’ll have a much clearer view of the matter if you pattern match and drop head, tail,thrd4, thirteenth17 etc

    knap _ []        = []
    knap x [(a,b,c)] = if x > c then [(a,b,c)]  else [(a, b, x)]
    knap x abcs      = knap2 x abcs []
    
    knap2 0 abcs z = z
    knap2 x abcs z = undefined  -- not sure how to do this
    
    -- but this makes sense, it seems:
    knap3 0 _  zs = zs
    knap3 _ [] _ = []
    knap3 x ((a,b,c,d):abcds) zs =
      if c > x then [(a, b, x)]
               else knap3 (x - c) abcds (zs ++ [(a, b, c)]) 
    

    or something like that. Instead of writing if length y == 1 you can pattern match on the singleton case; instead of using an equality test, if x == 0 you can pattern match on the 0 case, distinguishing it from other cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to solve this exponentiation problem . Here is the code that
Here's an interesting problem to solve in minimal amounts of code. I expect the
I'm trying to solve the problem here but I don't know why my code
Here is code my jqgrid editing through form. $(#DataEnergy).jqGrid('navGrid', '#pagergrid', {}, //options {editdata: {
Here a code to demonstrate an annoying problem: class A { public: A(): m_b(1),
I have a slight problem here I try to solve. As I start to
In basic Java, if I write code to solve a common problem and want
i face problem with System.currentTimeMillis( ) in my project i write some code here
So i have a problem. I found a cool old tv grain code (here
I've got problem here which I couldn't solve by myself. Everything was okay before,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.