# Why doesn't \< evaluate the arguments?

**URL:** <https://swi-prolog.discourse.group/t/why-doesnt-evaluate-the-arguments/6792>\
**Category:** Help!\
**Created:** [August 24, 2023, 10:36pm UTC](https://swi-prolog.discourse.group/t/why-doesnt-evaluate-the-arguments/6792 "2023-08-24T22:36:46Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [August 24, 2023, 11:28pm UTC](https://swi-prolog.discourse.group/t/why-doesnt-evaluate-the-arguments/6792/2 "2023-08-24T23:28:49Z")

</div>

The evaluation for (\<)/2 was being done, but unification _doesn’t_ evaluate:

```prolog
gcd(X, X, X). % does not evaluate X

```

Instead, you need to do something like this for the first clause:

```prolog
gcd(X, Y, D) :-
    X =:= Y % evaluate
    D is X. % also evaluate

```

So, as a general rule, it’s best to use is/2 to evaluate an expression rather than passing unevaluated expressions around.

BTW, this is how I’d write it (using if-then-else) without using is/2:

```prolog
gcd(X, Y, Gcd) :-
    ( X =:= Y
    -> Gcd is X
    ; X < Y
    -> gcd(X, Y-X, Gcd)
    ; % Y < X
        gcd(Y, X-Y, Gcd)
    ).

```

and using if-then-else and is/2:

```prolog
gcd(X, Y, D) :-
    ( X = Y
    -> D = X
    ; X < Y
    -> Z is Y-X,
        gcd(X, Z, D)
    ; % Y < X,
        Z is X-Y,
        gcd(Y, Z, D)
    ).

```

The if-then-else avoids creating choicepoints.

---

_[View the full topic](https://swi-prolog.discourse.group/t/why-doesnt-evaluate-the-arguments/6792)._
