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

  • Home
  • SEARCH
  • 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 8318823
In Process

The Archive Base Latest Questions

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

I am trying to do the fractional knapsack problem in haskell so far I

  • 0

I am trying to do the fractional knapsack problem in haskell so far I have

Code:

{- Input "how much can the knapsack hole <- x" "Possible items in sack [(label, value, weight), ...]" -}
knap x [] = []
knap x y = if length y == 1 then 

The input list is in the form [([Char], Integer, Integer), … ] a list of lists of (list of chars, integer, and integer).

My problem is trying to pull out the label, value, and weight of each item that could possibly be put in the knapsack. (pulling values out of the list of lists)

In my prelude> prompt I am doing some trying by doing

ghci output:

Prelude> let x = [("label 1", 2, 14), ("label 2", 1, 15)]
Prelude> :t x
x :: [([Char], Integer, Integer)]
Prelude> length x
2
Prelude> x !! 0
("label 1",2,14)
Prelude> x !! 0 !! 1

<interactive>:1:1:
    Couldn't match expected type `[a0]'
                with actual type `([Char], Integer, Integer)'
    Expected type: [[a0]]
      Actual type: [([Char], Integer, Integer)]
    In the first argument of `(!!)', namely `x'
    In the first argument of `(!!)', namely `x !! 0'

As you can see I am trying to do list !! index !! index to try and pull a weight off of a “item”. What is the proper syntax to do this?

  • 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:06:42+00:00Added an answer on June 8, 2026 at 10:06 pm

    Well, the !! operator only works on lists, as you can see from its type signature: [a] -> Int -> a.

    If you’d like to stick with tuples, you could define your own functions for 3-tuples in the style of fst and snd. You could do this with pattern matching; something like:

    first :: (a,b,c) -> a
    first (a,_,_) = a
    

    However, it might be better to design a datatype for an item, and use records to extract the field you need.

    data Item = Item { label  :: String
                       value  :: Int
                       weight :: Int
                     }
    

    Then to make a new item x you can use let x = Item {label = "label 1", value = 2, weight = 14}

    Now you could model your knapsack as a list of items of type [Item], and to get the value of the first item, you could use value $ knapsack !! 0, where knapsack is your list of items.

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

Sidebar

Related Questions

I have some functional code that I'm trying to speed up by eliminating the
I'm trying to think functional, in XSLT terms, as much as possible, but in
I'm trying to solve this problem in a pure-functional way, without using set! .
Sometimes I still get stuck trying to translate procedural code into functional code. Is
I am trying to understand what a fractional second is. I found a fractional
I'm trying to understand how this functional reactive programming works, and I have run
I'm trying to write my own functional little lib in Java. If I have
I have been trying to wrap my head around functional programming for a while
I have an issue using the Location Api. I'm trying to get the Location
I'm trying to wrap my head around functional programming concepts. Consider the problem of

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.