i need a predicate that will produce all the binary number of N digits .
For instance the predicate binary(2,L)
will return L = [[0, 0], [0, 1], [1, 0], [1, 1]]
.
please do not use findall ....
i need a predicate that will produce all the binary number of N digits .
For instance the predicate binary(2,L)
will return L = [[0, 0], [0, 1], [1, 0], [1, 1]]
.
please do not use findall ....
If you need to avoid
findall/3
, then you need an aggregator to collect the binary numbers:You then generate one binary at a time and check whether it's already present in the aggregated list:
If generating another binary fails, you are done:
Generating binaries is simple (I'm using the format you gave in your question: a list of 0/1 values). You iterate over all positions in the list and use either 1 or 0:
Result:
Once you have a list representing all the numbers with N bits, generating all the numbers of N+1 bits is just a matter of unfolding every N-number
[a,b,c,...]
into two N+1-numbers:[0,a,b,c,...]
and[1,a,b,c,...]
.Update: