100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Prolog Cheat Sheet

Prolog Cheat Sheet

Core Prolog concepts covering facts, rules, queries, list operations, and built-in predicates for logic programming.

2 PagesAdvancedApr 8, 2026

Facts & Rules

Defining facts and a derived rule.

prolog
% Factsparent(tom, bob).parent(bob, ann).parent(bob, pat).% Rule: X is a grandparent of Z if X is a parent of Y% and Y is a parent of Zgrandparent(X, Z) :- parent(X, Y), parent(Y, Z).% Rule with a not-equal checksibling(X, Y) :- parent(P, X), parent(P, Y), X \= Y.

Queries

Asking questions against the knowledge base.

prolog
?- parent(tom, bob).true.?- grandparent(tom, X).X = ann ;X = pat.?- parent(X, Y), parent(Y, ann).X = tom,Y = bob.

List Operations

Working with Prolog lists.

  • [H|T]- Head/tail list decomposition pattern
  • length(List, N)- N unifies with the length of List
  • append(L1, L2, L3)- L3 is L1 concatenated with L2
  • member(X, List)- True if X occurs in List
  • reverse(L1, L2)- L2 is L1 reversed
  • nth0(Index, List, Elem)- Elem is the element at the 0-based Index

Built-in Predicates

Commonly used core predicates.

  • =- Unification of two terms
  • \=- True if two terms do not unify
  • is/2- Arithmetic evaluation, e.g. X is 2 + 3
  • !- Cut; commits to choices made so far and prunes backtracking
  • findall(Template, Goal, List)- Collects all solutions of Goal into List
  • write/1, nl/0- Print a term / print a newline

Recursion Example

A classic recursive factorial predicate.

prolog
factorial(0, 1).factorial(N, F) :-    N > 0,    N1 is N - 1,    factorial(N1, F1),    F is N * F1.?- factorial(5, X).X = 120.
Pro Tip

Use the cut (!) sparingly and only after you understand its effect on backtracking — an overused cut silently discards valid solutions and makes predicates hard to reuse.

Was this cheat sheet helpful?

Explore Topics

#Prolog#PrologCheatSheet#Programming#Advanced#FactsRules#Queries#ListOperations#BuiltInPredicates#DataStructures#Databases#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet