Вопрос

I'm new to Prolog and I'm trying to resolve this exercise:

Define a predicate greater_than/2 that takes two numerals in the notation that we introduced in this lecture (i.e. 0, succ(0), succ(succ(0))...) as arguments and decides whether the first one is greater than the second one. E.g:

?- greater_than( succ(succ(succ(0))), succ(0) ).
yes.
?- greater_than( succ(succ(0)), succ(succ(succ(0))) ). 
no.

This is my answer so far:

greater_than(X, 0).
greater_than( succ(succ(X)), succ(Y) ).

but of course doesn't work properly, so I'm asking anyone for help. Thanks.

Это было полезно?

Решение

As you are looking for a recursive solution, you have to provide a base case and a recursive step. The base case you provided is almost right. However it fails because it will succeed for example when both numbers are zero. It should succeed only when the left side is of the form succ(_) and the right side is zero.

The recursive step should take an element from each side and apply recursion. Therefore this should work:

greater_than(succ(_), 0).
greater_than(succ(X), succ(Y)):-
  greater_than(X, Y).
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top