Return number and successive with backtracing - PROLOG

0

The predicate that I want to implement, I need you to do the following:

predicado(Máximo,Num1,Num2).

And you must return two numbers Num1 and Num2 fulfilling that:

  • Num1 < Num2
  • Num2
asked by Pablo 08.12.2016 в 18:49
source

1 answer

0

You simply need a function that generates a range of numbers by re-evaluation:

ran(X,Y,X) :- X < Y.
ran(X,Y,Z) :- X < Y, X2 is X+1, ran(X2,Y,Z).

Now you can implement your predicate based on two ranges:

  • From 1 to maximum ( X+1 ), which will be the second number ( Z )
  • From 1 to Z , which will be the first number ( Y )

That is:

p(X,Y,Z) :- X2 is X+1, ran(1,X2,Z), ran(1,Z,Y).
    
answered by 08.12.2016 / 22:46
source