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

The Archive Base Latest Questions

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

I have a problem, I have a list with numeric elements such as in

  • 0

I have a problem,
I have a list with numeric elements such as in the example.
I´d like to find all pairs, and count it. (Every Element can only be one part of one pair)

?- num_pairs([4,1,1,1,4],N).
N=1;

Can anyone help me to solve this problem??

  • 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-15T19:52:28+00:00Added an answer on June 15, 2026 at 7:52 pm

    You need several things to make it work:

    • An ability to count the number an item is repeated in a list
    • An ability to remove all elements matching a value from the list
    • An ability to conditionally increment a number

    Here is how you can count:

    count([], _, 0).
    count([H|T], H, R) :- count(T, H, RT), R is RT + 1.
    count([H|T], X, R) :- H \= X, count(T, X, R).
    

    Deletion can be done with SWI’s delete/3 predicate; this is a built predicate.

    Adding one conditionally requires two rules – one when the count equals one, and another one for when the count does not equal one.

    add_if_count_is_one(H, T, RT, R) :- count(T, H, 1), R is RT + 1.
    add_if_count_is_one(H, T, R, R) :- count(T, H, X), X \= 1.
    

    Finally, counting pairs could look like this:

    num_pairs([], 0).
    num_pairs([H|T], R) :- delete(T, H, TT),
                           num_pairs(TT, RT),
                           add_if_count_is_one(H, T, RT, R).
    

    An empty list has no pairs; when an item is counted as part of a pair, its copies are removed from the rest of the list.

    Here is this running program on ideone.

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

Sidebar

Related Questions

I have problem with adding class to every list element. This is example html:
i have the following problem I have a list with strings for example (100_1,
My problem is this. I have lists of different numeric types, for example: QList<qreal>
Background Now, I have a data frame shaped like: example = structure(list(sid = c(39,
I have problem with select list in IE (all version). I got two field
I have a problem with list and listview. I started a project and added
I have the following problem: list.c struct nmlist_element_s { void *data; struct nmlist_element_s *next;
I have problem adding arraylist to list view, will explain about my problem here..
I have a problem displaying zebra list after filtering results. I have a zebra
I have a problem with an list-component on the sencha touch 2 framework. My

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.