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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:45:00+00:00 2026-05-30T05:45:00+00:00

This is the question for one of my assignments: Write repCount(L, X, N) which

  • 0

This is the question for one of my assignments:

Write repCount(L, X, N) which is true when N is the number of occurrences of X in list L.

Here’s my code where I try to tackle the problem recursively:

repCount([], X, N) :-
   N is 0.
repCount([H|T], X, N) :-
   count([H|T], X, N).

count([], X, 0).
count([H|T], X, N) :-
   count(T, X, N1), 
   X =:= H,
   N is N1 + 1.

And it works when I supply a list full of identical numbers like this:

?- repCount([2,2,2], 2, N).
N = 3.

But if I supply a list with at least one different value:

?- repCount([2,2,22], 2, N).
false.

It returns false. I cannot figure out why this happens or how to change it to ‘skip’ the non-matching value, rather than declare the whole thing false. Any input is appreciated.

  • 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-05-30T05:45:01+00:00Added an answer on May 30, 2026 at 5:45 am
    count([H|T], X, N):- count(T, X, N1), X=:=H, N is N1 + 1.
    

    here you declare that N should be N1+1 if X is H; however you do not define what should happen if X is not H (basically missing an else clause)
    this should work:

    count([H|T], X, N):- 
        count(T, X, N1), 
        (X=:=H->
           N is N1 + 1
           ; N is N1).
    

    another way would be:

    count([H|T], X, N):- count(T, X, N1), X=:=H, N is N1 + 1.
    
    count([H|T], X, N):- X=\=H, count(T, X, N1), N is N1.
    

    but this is inefficient since count(T,X,N1) will be called twice if X is not H. we can fix this by doing the check in the head of the clause:

    count([H|T], H, N):- count(T, X, N1), N is N1 + 1.
    
    count([H|T], X, N):- count(T, X, N1), N is N1.
    

    or simply:
    count([H|T], H, N):- count(T, X, N1), N is N1 + 1.

    count([H|T], X, N1):- X=\=H, count(T, X, N1).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like an easy question. One of my assignments basically sends a time
So I got this question from one of the developers in my team: What
I just saw this question and one of the answers looks really appealing to
I was recently asked this question in one of my telephonic interview. There is
The current top-voted to this question states: Another one that's not so much a
This question is related to this one , though I think I was a
This question is similar to this one How do I add options to a
This question is related with one of my earlier questions.. Previous Post In there
This question might belong on one of the other trilogies, but it sorta seemed
This question repeats my earlier one but my earlier one was a failure because

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.