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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:48:22+00:00 2026-06-11T09:48:22+00:00

I have a problem with the recursive function of Prolog. I believe I am

  • 0

I have a problem with the recursive function of Prolog. I believe I am not implementing it right and need help.

I need to generate the first N prime numbers and return it in a list. Generating the prime number is not an issue, but rather, generating it in a list is the issue I have.

This is the part of the relevant code:

genList(_, 0, _).
genList(X, N, PrimeList, PrimeList):-
    N > 0,
    isprime(X),
    X1 is X +1,
    N1 is N -1,
    genList(X1,N1,[X|PrimeList], [X|PrimeList]),!.
genList(X, N, PrimeList, PrimeList):-
    N>0,
    \+isprime(X),
    X1 is X + 1,
    genList(X1,N,PrimeList, PrimeList).

This is what I type into the Prolog interpreter:

genList(1,N, [],L).

For the 1st line, how do I make the base case such that when N=0, I stop recursing? Is this correct?

As for the next 2 clauses, I am having difficulty in thinking in terms of logic programming. I definitely feel that this is not logic programming style.

I want to say that when isPrime(X) fails, we continue to the next number without saving anything, but when isPrime(X) is true, then we recurse and continue to the next number, saving X.

How do I do that in Prolog?

  • 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-11T09:48:24+00:00Added an answer on June 11, 2026 at 9:48 am

    First of all, you shouldn’t need 4 arguments to your main predicate if you only want two. Here you want the list of the first primes up to N. So an argument for N and an argument for the list should be enough:

    primeList(N, L) :-
        % eventually in the body a call to a worker predicate with more arguments
    

    Now here, your logic is explained in those terms:

    primeList(N, [N|L]) :-
        % If we're not at the base case yet
        N > 0,
        % If N is a prime
        isPrime(N),
        NewN is N - 1,
        % Let's recurse and unifie N as the head of our result list in the head
        % of the predicate
        primeList(NewN, L).
    
    primeList(N, L) :-
        % Same as above but no further unification in the head this time.
        N > 0,
        % Because N isn't a prime
        \+ isPrime(N),
        NewN is N - 1,
        primeList(NewN, L).
    

    To that you’d have to add the base case

    primeList(0, []).
    

    You could rewrite that with cuts as follows:

    primeList(0, []) :- !.
    primeList(N, [N|L]) :-
        isPrime(N),
        !,
        NewN is N - 1,
        primeList(NewN, L).
    primeList(N, L) :-
        NewN is N - 1,
        primeList(NewN, L).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a recursive function being used to generate a menu on my site.
Recursive function for copy of multilevel folder is not working. I have a code
I have a problem with a recursive SQL function. The original problem is that
I have a recursive function (in C#) that I need to call about 800
I have a problem with my recursive function in PHP. The aim of my
I have a big problem that a recursive treeview doesn't update any childs if
Had a problem with the recursive conflictCheck() method. That seems fine now. I have
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have a problem in postgres function: CREATE OR REPLACE FUNCTION linkedRepoObjects(id bigint) RETURNS
I have a recursive function to iterate over 11M database records, 1000 at a

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.