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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:53:20+00:00 2026-05-29T18:53:20+00:00

I want to calculate , in two lists (same lenght), the number of elements

  • 0

I want to calculate , in two lists (same lenght), the number of elements that are equal and in the same position.
For example:
Lets say we have the lists A = [3,6,7,9] and B = [2,6,4,9], i want to be printed in the screen the message, “2 bulls found”.

So far i have made this:

bulls([],[]).
bulls([Ha|Ta],[Hb|Tb]) :-
    Ha = Hb,
    writeln('bull found'),
    bulls(Ta,Tb);
    bulls(Ta,Tb).

Every time an element that exists in the same place in both lists, the message ‘bull found’ is printed.
And in my mind i want to make something like this:

bulls([],[],_).
bulls([Ha|Ta],[Hb|Tb],Counter) :-
    Ha = Hb,
    NewCounter is Counter + 1,
    bulls(Ta,Tb,NewCounter);
    bulls(Ta,Tb,NewCounter).

bulls(List1,List2):- bulls(List1,List2,0).

bulls is called from another rule that passes the lists two it.
How do i make it so it prints the value of ‘bulls’ to the screen. Any help?


Edit
So after Suki’s post, i made this test program testing 2 lists:

bulls([],[],X), write(X), write('bulls found'),fail.
bulls([Ha|Ta],[Hb|Tb],Counter) :-
    Ha = Hb,
    NewCounter is Counter + 1,
    bulls(Ta,Tb,NewCounter);
    bulls(Ta,Tb,NewCounter).

check(List1,List2):- 
    bulls(List1,List2,0).


start:-
    A=[1,1,1,1],
    B=[2,1,2,1],
    writeln(A),writeln(B),
    check(A,B).

and i get this result

1 ?- start.
[1,1,1,1]
[2,1,2,1]
ERROR: bulls/3: Arguments are not sufficiently instantiated

what am i doing wrong?

  • 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-29T18:53:23+00:00Added an answer on May 29, 2026 at 6:53 pm

    Regarding your edited program:

    The first clause is not a clause, it’s a goal! It should look like this:

    bulls([],[],X) :- write(X), write(' bulls found').
    

    You should drop the fail, btw.

    In the second clause, you need to use “if-then-else”, and use Counter instead of NewCounter in the “else”-branch:

    bulls([Ha|Ta],[Hb|Tb],Counter) :-
      (
        Ha == Hb
      ->
        NewCounter is Counter + 1,
        bulls(Ta,Tb,NewCounter)
      ;
        bulls(Ta,Tb,Counter)
      ).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to calculate the angle between two vectors a and b. Lets assume
I have two objects of Zend_Date class and I want to calculate the difference
I have a point in 3D space and two angles, I want to calculate
I have two lists of fractions; say A = [ 1/212, 5/212, 3/212, ...
I have two lists of fractions; say A = [ 1/212, 5/212, 3/212, ...
I have a design that I want to position the views into these section
I have two python lists of floats (lists are of same size i.e same
I have two arrays containing the same elements, but in different orders, and I
I want to calculate the difference between two times, one of which is the
I only want to calculate an AVG of values if two or more non-zero

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.