user4067810
user4067810

Reputation:

Modulus in 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?

Upvotes: 4

Views: 33678

Answers (1)

Shevliaskovic
Shevliaskovic

Reputation: 1564

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.

Upvotes: 5

Related Questions