'Returning a value from a function in prolog

How do we return a value from a Prolog function that takes only one argument? Ex. leaf(V) is a function which should return a value V and it is called from a function tree(leav(V), sum)?



Solution 1:[1]

Prolog does not have function evaluation, so you will have to represent your function as a relation

f(In,Out) :- ... define the relationship between In and Out in the body ...

For example for squaring:

square(X,Y) :- Y is X*X.
?- square(12,X).
X=144

The "function symbols" in Prolog are purely denotational: they are for constructing terms:

f(x)

The above term has no inherent meaning or resolvability. It is just a term, more precisely a tree

f
|
x

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Tonhofer