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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:12:52+00:00 2026-06-15T03:12:52+00:00

I need to traverse a list and do some calculations with every element and

  • 0

I need to traverse a list and do some calculations with every element and the elements excluding that element. For example, having a list (1 2 3 1), I need pairs (1) (2 3 1), (2) (1 3 1), (3) (1 2 1) and (1) (2 3 1).

  • 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-15T03:12:53+00:00Added an answer on June 15, 2026 at 3:12 am

    (…) with every element and the elements excluding that element.

    With every element sounds like a map. Excluding that element sounds like a filter. Let’s start with the latter.

    user=> (filter #(not= % 1) '(1 2 3))
    (2 3)
    

    Great. Now let’s try to map it over all elements.

    user=> (let [coll '(1 2 3)] (map (fn [elem] (filter #(not= % elem) coll)) coll))
    ((2 3) (1 3) (1 2))
    

    Creating actual pairs is left as an exercise for the reader. Hint: modify the closure used in map.

    Keep in mind that the solution suggested above should work fine for short lists but it has a complexity of O(n²). Another issue is the fact that collections with duplicates aren’t handled correctly.

    Let’s take a recursive approach instead. We’ll base the recursion on loop and recur.

    (defn pairs [coll]
      (loop [ahead coll behind [] answer []]
        (if (empty? ahead)
          answer
          (let [[current & remaining] ahead]
            (recur remaining
                   (conj behind current)
                   (conj answer [(list current)
                                 (concat behind remaining)]))))))
    

    A trivial example:

    user=> (pairs '(1 2 3))
    [[(1) (2 3)] [(2) (1 3)] [(3) (1 2)]]
    

    A vector with duplicates:

    user=> (pairs [1 5 6 5])
    [[(1) (5 6 5)] [(5) (1 6 5)] [(6) (1 5 5)] [(5) (1 5 6)]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to delete an element from linked list where address of that element
I need to write a regex, that will traverse through my file and replace
I have a list of links/image pairs in a page. For example, social page
Are there advantages to either approach? If I need to traverse List items and
I am trying to write some code that will loop through a list and
I have got stuck in some issue where I need to traverse through a
I am developing a daily calendar and need to traverse to the next and
I'm developing an app in Android and I need to traverse a xml file.
Need some regular expressions help. So far I have my code working to allow
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via

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.