I am trying to solve this puzzle in prolog:
A bat and ball cost $1.10. The bat costs one dollar more than the ball. How much does the ball cost?
solve(Bat, Ball, Sum) :-
Bat + Ball is Sum,
Bat is 1 + Ball.
I came up with this potential solution, however I am getting false with no answer.
Here's my query:
solve(Bat, Ball, 1.10).
I would also like to understand, what's wrong with my reasoning here.
Bat + Ball is Sum
...is/2
doesn't work this way.is/2
evaluates the 2nd argument and binds it to the 1st. So the first must be a single, bindable variable. Anything else will fail automatically. No the other way around. Also, it doesn't know how to solve for multiple variables. For that, use CLP(FD):Bat + Ball #= Sum
andBat #= 1 + Ball
. You'll need the directive,:- use_module(library(clpfd)).
?- solve(Bat, Ball, 110).
answer:1+Ball#=Bat, Bat+Ball#=110.
. I would like to get answer for example -Bat: 105, Ball: 5