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 8799193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:15:40+00:00 2026-06-14T00:15:40+00:00

My homework task is to group two tuples in a list if the second

  • 0

My homework task is to group two tuples in a list if the second element of the first tuple is the same as the first element of the second tuple. Then, if the first tuple is (a, b) and the second is (b, c), the tuple (a, c) must be added to the result list.

I wrote first function wich takes element with one tuple and second list with many tuples and compare each to each.

this one works correcly:

c1 = ("a","x")
d1 = [ ("x","b"), ("z","c"), ("x","b"), ("z","c")
     , ("x","b"), ("z","c"), ("x","b"), ("z","c") ]

getByOne c1 a1 = filter (/=[])
  [ if (fst  (last(take n a1))) == (snd c1)
    then [((fst c1),  (snd  (last(take n a1))))]
    else  [] | n <- [1..(length a1) ] ]

output:

[ [("a","b")], [("a","b")], [("a","b")], [("a","b")] ]

But the problems is that I can’t throw in if then and else statement just simple tuple, so I create a new list. In the end of this “workaround” i am getting list in list in list and so on. Also if output list was bigger, there were be more lists in lists.

Is there any way to pass out only tuple or empty tuple or i should somehow group theses lists ?

  • 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-14T00:15:41+00:00Added an answer on June 14, 2026 at 12:15 am

    You can flatten the result using

    concat :: [[a]] -> [a]
    

    then you don’t even need to filter (/=[]) – by the way, the condition (/= []) is more idiomatically written not . null, since the null test doesn’t impose an Eq constraint on its argument (you already have one here, so it’s just a matter of idiom).

    Further, last (take n a1) is just the n-th element of a1 if 1 <= n <= length a1. Since you have that restriction imposed, that can more succinctly be expressed as a1 !! (n-1)

    Then you have structurally

    getByOne c1 a1 = concat $ [something c1 (a1 !! i) | i <- [0 .. length a1 - 1]]
    

    (I have shifted indices to index by i), which is clearer and more efficiently expressed as

    getByOne c1 a1 = concat $ map (something c1) a1
    

    If you prefer a list comprehension to map, you can also write that as

    getByOne c1 a1 = concat [something c1 x | x <- a1]
    

    which in your case, using the ability to pattern-match in alist-comprehension generator, gives us

    getByOne (f,s) a1 = concat [ if a == s then [(f,b)] else [] | (a,b) <- a1]
    

    which is much shorter and more readable. Instead of using if condition then [element] else [] and concat, even nicer is to use a condition in the list comprehension,

    getByOne (f,s) list = [(f,b) | (a,b) <- list, a == s]
    

    which is short and clear.

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

Sidebar

Related Questions

Not homework. Given two strings S and T of same length. Given a set
Part of a task for a homework is to load two text-files and save
This is a homework task, but it's very simple. The task comes with a
I have a homework task to prove whether or not a particular variation on
This task was actually given to us as homework, but the requirement in the
This is not a homework. Just an interesting task :) Given a complete binary
I have a PROGRAMMING task (homework) to investigate brute force neural net methods. That
This is a homework if anyone's curious. The task is to have a C
I have homework that should use fork() and wait() system calls: 1) Write two
From C Primer Plus 5th Ed: Write a program that creates two eight-element arrays

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.