AL-Duais
AL-Duais

Reputation: 11

Prolog predicate (division) that calculate the expression

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

Answers (1)

lambda.xy.x
lambda.xy.x

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

Related Questions