Multiple Return Values in Prolog
In Prolog, we can achieve similar functionality using predicates. Here’s how we can implement multiple return values:
In this Prolog code:
We define a predicate
vals/2
that succeeds with the values 3 and 7. This is analogous to thevals()
function in the original code.The
main/0
predicate is our entry point, similar to themain()
function.We use
vals(A, B)
to “return” two values, which are unified with the variablesA
andB
. This is similar to the multiple assignment in the original code.We use
write/1
andnl/0
to print the values and new lines, which is analogous tofmt.Println()
.To demonstrate ignoring a value, we use an anonymous variable
_
invals(_, C)
. This is similar to the use of the blank identifier in the original code.
To run this Prolog program:
In Prolog, predicates either succeed or fail, and can have multiple solutions. This is a different paradigm from functions returning values, but it allows for similar and even more flexible patterns of data flow.
The concept of multiple return values in Prolog is implicit in the way predicates work with multiple arguments. While it’s not exactly the same as in other languages, it provides a powerful way to work with multiple pieces of data in a single predicate call.