I'm trying to create a list in Prolog (SWI Prolog) and check which numbers are powers of 2 and second find how many times a specific number is in the list (in this example I'm trying to find how many times the number 3 is in the list). For a example, if you ask
?- check([0,2,3,-5,-2,1,8,7,4], MULT2, THREE).
you should see
MULT2=[2,8,4]
THREE=1
My first try to find a solution is to search the list with head and doing head mod 2 = 0 to find all numbers which are powers of 2, but something went wrong and I only get "false" as an answer.
Here's how you can find the "powers of two" in logically-pure way!
Using sicstus-prolog 4.3.5,
library(reif)andlibrary(clpz):Sample query1 using meta-predicate
tfilter/3in combination withpower_of_two_t/2:Here's a more general query suggested by a comment:
Footnote 1: The answer sequences shown above were brushed up to indicate the determinism of calls.
Footnote 2: To reproduce the results use
call_det/2which is defined like this: