0
votes

I'm newbie to prolog and trying to understand this prolog code.

next_truth_value([0|R],[1|R]).
next_truth_value([1|R],[0|S]) :- next_truth_value(R,S).

Through my research I found that predicate contains two lists. If I query this I get a answer like this...

?- next_truth_value([0,0], NEXT).
NEXT = [1,0]

Please someone explain this code, I'm totally helpless about understanding what this really meant. Thank You.

1
So where did you find this? What is it really supposed to be doing? There must have been some more context to this than you are giving us.... (and to understand what exactly is going on, you can try tracing: ?- trace. on the top-level, although many people consider tracing a Prolog program a sign of weakness).user1812457
First of all thanks for the reply. It generates next truth value. let's say we have given (0,0), it will generate next truth value as (1,0)... here is the full code... sourceforge.net/p/prologassignmentnsbmcolombo/code/ci/master/…bynu022
So do you understand the rest of the code? Who wrote it? Can't you ask them?user1812457

1 Answers

2
votes

This code is calculating the next binary number (reversed). If you have a binary number 00, the next binary number is 01 (which is represented as [1,0]). Your predicate will change a zero into a one, but if there is already a one (like in the binary number 01), it will have to look further in the list to find a zero to turn into a one. This code will by the way not work for the list [1,1], it will return false.

You could also use the same code to find the previous binary number. If you use the predicate like this: next_truth_value(Prev,[1,0])., you will get the result [0,0].