New to Haskell and have a stumbling block. I’m trying to filter a list of tuples based on the first item.
filter (==(x,_)) lis
I get an illegal ‘_’ error, but I’m not sure how I can get around it?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
In Haskell, you cannot iterate over a tuple like you can a list.
If the tuple only has two items, you can use
fstto retrieve the first item of the tuple andsndto retrieve the second item.One way to do what I think you want to do is this approach:
Which only returns the items in the list where the first element is equal to 1; of course, you can substitute
xwhere I put 1.To be a little more specific,
(==1).fstfirst appliesfstto the element in lst, then applies(==1)to the result offst— technically, the dot composes the two functions together.