Prolog Beginner: How to unify with arithmentic comparison operators or how to get a set var to range

Posted by sixtyfootersdude on Stack Overflow See other posts from Stack Overflow or by sixtyfootersdude
Published on 2010-06-05T16:55:38Z Indexed on 2010/06/06 8:52 UTC
Read the original article Hit count: 210

Filed under:
|
|

I am new to prolog. I need to write an integer adder that will add numbers between 0-9 to other numbers 0-9 and produce a solution 0-18. This is what I want to do:

% sudo code
add(in1, in2, out) :-
    in1 < 10,
    in2 < 10,
    out < 18.

I would like to be able to call it like this:

To Check if it is a valid addition:

?- add(1,2,3).
true
?- add(1,2,4).
false

With one missing variable:

?- add(X,2,3).
1
?- add(1,4,X).
5

With multiple missing variables:

?-add(X,Y,Z).
% Some output that would make sense.  Some examples could be:
X=1, Y=1, Z=2 ;
X=2, Y=1, Z=3 ......

I realize that this is probably a pretty simplistic question and it is probably very straightforward. However cording to the prolog tutorial I am using:

"Unlike unification Arithmetic Comparison Operators operators cannot be used to give values to a variable. The can only be evaluated when every term on each side have been instantiated."

© Stack Overflow or respective owner

Related posts about beginner

Related posts about prolog