Quantcast
Channel: Active questions tagged return-value - Stack Overflow
Viewing all articles
Browse latest Browse all 207

How to create a list dynamically in Prolog?

$
0
0

I have looked at the gazillions of other similar questions, but I haven't found a working solution, so I am asking this:

I have a rule:

check_prime(X) :-  X > 0,  X0 is X - 1,   (X =:= 1 -> true;    X =:= 2 -> true;    foreach(between(2, X0, T), X mod T =\= 0) -> true; false).

then I want to run:

B is [0], foreach( between(1, 50, T),          (check_prime(T) -> B2 = [B, T], write(B2); !)).

What this successfully does is write pairs of [0, primeNumber] to the console. I can alternatively do this:

B is [0],foreach( between(1, 50, T),          (check_prime(T) -> (write(T), write(", ")); !)).

which actually prints everything in a pretty way, minus the lack of being a list, and the extra comma at the end.

This writing function is working through the solutions of check_prime(T) recursively, one solution at a time. I want to make a list of all of these solutions, but no matter what I try, I either cannot get object permanence (the written atom is something like _31415926), or I encounter a variety of errors.

How do I dynamically create a list of prime numbers since each one is found recursively?

Edit:

Yes, this is all the code I am using:enter image description here

enter image description here

I have tried varying the "if true" branch, but it keeps having issues:

enter image description hereenter image description here

enter image description here


Viewing all articles
Browse latest Browse all 207

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>