[SOLVED] Returning a value from a function in prolog

Issue

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

Prolog doesn’t have “functions”, it has “predicates”. I.e. they are a description of a relationship between terms. A predicate can only evaluate to “true” or “false”.

However, the way a predicate evaluates to true or false is by unification, i.e. prolog tries to find instances for the variables from the existing knowledge base (i.e. your source code) that will make that predicate true.

Therefore you can trick a prolog predicate of the form Pred(In, Out), such that, for a given “In” prolog could unify “Out” with the right value that makes the predicate true.

Answered By – Tasos Papastylianou

Answer Checked By – David Marino (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.