P. 1
Prolog Tutorial

# Prolog Tutorial

|Views: 323|Likes:

See more
See less

02/25/2013

pdf

text

original

## Sections

• Contents
• Preface
• What is Prolog?
• What are strengths and Weaknesses?
• Section 1: Introduction
• The Compiler
• Horn Clause Logic
• Extending the Family Theory
• Prolog is a Programming Language
• Failing
• Backtracking
• Improving the Family Theory
• Recursion
• Side Effects
• Conclusion
• Section 2: A First Example
• Open Visual Prolog
• Section 3: Getting Started
• Typing in a Prolog program
• Starting Prolog
• Running a query
• Section 4: Facts and Rules
• The Rules
• The Family Tree Example
• Section 5: Operators and Arithmetic
• Some Prolog Details
• Arity
• Spaces
• Simple I/O in Prolog
• Arithmetic in Prolog
• Built-In Predicates
• Arithmetic Operators
• Some queries:
• Exercises
• Section 6: Recursion
• Using Recursion
• Some Examples
• Exercise:
• The Towers of Hanoi
• The Grid Example
• Section 7: Structures
• The General Form of a Structure
• Arithmetic "Functions" and Structures
• A simple example of using structures
• Section 8: Recursive Structures
• Inserting an element
• Binary Trees
• Exercise
• Section 9: Introducing Lists
• Format of Lists
• Empty and Non-Empty Lists
• The length of a list
• Summing a list
• List Membership
• Section 10: Lists as Accumulators
• Collecting information
• Joining two lists
• Reversing a List
• Built-In list predicates
• Section 11: Backtracking and Cut
• Analysing Cases
• An Example Of Using The Cut
• The First Cut
• Another Cut
• Yet Another Cut
• Section 12: More Control Features
• Kinds of cut
• Green cuts
• Red cuts
• Negation as Failure
• Warning!
• If-then-else in Prolog
• The repeat predicate
• Section 13: Input and Output
• More on I/O
• File I/O
• Saving and Restoring a Knowledge-Base
• Other Approaches to I/O
• An Exercise

A list is simply an ordered, extendable sequence of terms; they correspond (roughly) to vectors
in C++/Java.

Remember that lists, like anything else which represents objects in Prolog, are terms.
Thus we don't need to "declare" them, we just use them when needed.

We write a list in Prolog using the brackets "[" and "]", and separate the elements by
commas. As with any term, they must only appear in a clause as arguments to a
predicate.

Thus [john, mary, pat] is a list with three elements.

List elements do not all have to look the same: ['string', 6, mary, X] is also a valid list. In
fact, a list element may be any kind of term: that is, a constant, variable, structure, or
even another list.

scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->