Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modulus in Prolog

Tags:

prolog

I am trying to use a conditional statement if N is even do something otherwise do something else. Here is some part of my code where I am doing this:

(N,Lines,1):-write(N),nl,
(    mod(N,2) = 0 ->
 write('Hello'),nl,write('Again Hello')

;
    foo(N,Lines)

).

But even if I give N=6 it goes into foo(N,Lines) i.e else part. It is not going into if part. Can someone please tell me where I'm wrong?


1 Answers

Instead of writing it as mod(N,2) = 0 write 0 is mod(N,2) or 0 =:= mod(N,2) (Like mat said in the comments)

Like:

write(N),nl,
(    0 is mod(N,2) ->
 write('Hello'),nl,write('Again Hello')
;
    foo(N,Lines)
).

and if N=6, like in your example, I get:

6
Hello
Again Hello
true.

There are some examples on prolog arithmetics here.

like image 140
Shevliaskovic Avatar answered Nov 03 '25 01:11

Shevliaskovic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!