Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Prolog
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Loops and recursion === Iterative algorithms can be implemented by means of recursive predicates.<ref>{{cite book |last=Carlsson |first=Mats |url=https://books.google.com/books?id=dZimAwAAQBAJ&q=prolog%20failure%20driven%20loop%20%22iteration%22&pg=PA148 |title=SICStus Prolog User's Manual 4.3: Core reference documentation |date=27 May 2014 |publisher=BoD β Books on Demand |isbn=978-3-7357-3744-1 |via=Google Books}}</ref> Consider the <code>parent_child/2</code> predicate defined in the family relation program above. The following Prolog program defines the ''ancestor'' relation:<syntaxhighlight lang="prolog"> ancestor(X, Y) :- parent_child(X, Y). ancestor(X, Y) :- parent_child(X, Z), ancestor(Z, Y). </syntaxhighlight>It expresses that X is an ancestor of Y if X is parent of Y or X is parent of an ancestor of Y. It is recursive because it is defined in terms of itself (there is a call to predicate <code>ancestor/2</code> in the body of the second clause).
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)