samedi 14 novembre 2020

Returning a value from an if-statement in Prolog

I'm trying to write a simple code like this:

In pseudo code:

function(x,y):
if y = 0:
        return x
if x = 0:
        return y

I'm guess I'm supposed to use sth like

function(X,Y,Return) :-

as a starting point and "if y = 0:" would be

y =:= 0.

but how do I return X? I need to assign it to the Return variable, but how?

Edit: I think I've found it:

function(X,Y,Return) :- ( X =:= 0 -> Return = X ).

Aucun commentaire:

Enregistrer un commentaire