Reputation: 11
Write a Prolog predicate (division) that calculate the expression (C=A/B) and write the result, where B should not equal 0, A&B should be inputted from the user by using read predicate.
Upvotes: 0
Views: 162
Reputation: 4998
This is a reified variant of the exercise:
:- use_module(library(reif)).
:- use_module(library(lambda)).
main :-
write('Enter a: '),
read(A),
write('Enter b: '),
read(B),
if_( X^division_(A,B,C,X),
format('Result: ~f',[C]),
write('Division by zero.')).
division_(_A, B, _C, false) :-
0 is B.
division_(A, B, C, true) :-
B =\= 0,
C is A / B.
Upvotes: 2