Reputation: 1064
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.
Upvotes: 0
Views: 151
Reputation: 1997
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]
.
Upvotes: 2