@peter.ludemann Thanks for the comments. His code for max/3
was actually…
max(X, Y, X) :-
X >= Y.
max(X, Y, Y) :-
X < Y.
This works, but left a choicepoint. It was while I was working on this, and an exercise for the GCD of two numbers that you posted some code that helped me write it using if
.
I wrote my code based on the excellent help I’ve had here. I’m trying hard to avoid choicepoints where there is only one solution to a question (eg there’s only one maximum of two integers, only one GCD of two integers, etc), and what I learned about argument indexing led me to the code I wrote.
Thanks also for the pointer toward foldl/4
. I’ve come across this before, and have seen similar ideas in other languages, but haven’t yet become familiar enough with it (or most of the built-in predicates) to be confident in using it. Your example will help me explore it more.
Thanks again.