Learn Prolog - 10 Code Examples & CST Typing Practice Test
Prolog (Programming in Logic) is a high-level, declarative programming language focused on logic programming and symbolic reasoning. It is widely used in artificial intelligence, natural language processing, and rule-based systems, enabling developers to express knowledge and relationships rather than step-by-step instructions.
Learn PROLOG with Real Code Examples
Updated Nov 20, 2025
Code Sample Descriptions
Prolog Counter Simulation
:- dynamic(count/1).
:- dynamic(isDark/1).
count(0).
isDark(false).
updateUI :-
count(C),
isDark(D),
write('Counter: '), write(C), nl,
write('Theme: '), (D = true -> write('Dark') ; write('Light')), nl.
increment :-
count(C),
C1 is C + 1,
retract(count(C)),
assert(count(C1)),
updateUI.
decrement :-
count(C),
C1 is C - 1,
retract(count(C)),
assert(count(C1)),
updateUI.
toggleTheme :-
isDark(D),
D1 is (D = false -> true ; false),
retract(isDark(D)),
assert(isDark(D1)),
updateUI.
reset :-
retract(count(_)),
assert(count(0)),
updateUI.
% Simulate actions
updateUI,
increment,
increment,
toggleTheme,
decrement,
reset.
Demonstrates a simple counter simulation with theme toggle using Prolog facts and rules.
Prolog Simple Addition
add(A,B,Sum) :- Sum is A + B.
% Example
?- add(10,20,S), write(S), nl.
Adds two numbers and prints the result.
Prolog Factorial
factorial(0,1).
factorial(N,F) :- N > 0, N1 is N-1, factorial(N1,F1), F is N * F1.
% Example
?- factorial(5,F), write(F), nl.
Calculates factorial using recursion.
Prolog Fibonacci Sequence
fib(0,0).
fib(1,1).
fib(N,F) :- N > 1, N1 is N-1, N2 is N-2, fib(N1,F1), fib(N2,F2), F is F1 + F2.
% Example
?- fib(10,F), write(F), nl.
Generates Fibonacci numbers recursively.
Prolog Max of Two Numbers
max(A,B,A) :- A >= B.
max(A,B,B) :- B > A.
% Example
?- max(10,20,M), write(M), nl.
Finds the maximum of two numbers.
Prolog List Sum
sum_list([],0).
sum_list([H|T],Sum) :- sum_list(T,SumT), Sum is H + SumT.
% Example
?- sum_list([1,2,3,4,5],S), write(S), nl.
Sums elements of a list recursively.
Prolog Even Numbers Filter
even(X) :- 0 is X mod 2.
print_even([]).
print_even([H|T]) :- even(H), write(H), nl, print_even(T).
print_even([H|T]) :- \+ even(H), print_even(T).
% Example
?- print_even([1,2,3,4,5]).
Prints even numbers from a list.
Prolog Conditional Counter Increment
:- dynamic(count/1).
count(3).
increment_if_less_than_5 :- count(C), C < 5 -> C1 is C + 1, retract(count(C)), assert(count(C1)); true.
% Example
?- increment_if_less_than_5, count(C), write(C), nl.
Increments counter only if less than 5.
Prolog Resettable Counter
:- dynamic(count/1).
count(0).
increment :- count(C), C1 is C + 1, retract(count(C)), assert(count(C1)).
reset :- retract(count(_)), assert(count(0)).
% Example
?- increment, increment, count(C), write(C), nl, reset, count(C2), write(C2), nl.
Counter that can increment and reset.
Prolog Theme Toggle Only
:- dynamic(isDark/1).
isDark(false).
toggle_theme :- isDark(D), D1 is (D = false -> true ; false), retract(isDark(D)), assert(isDark(D1)).
% Example
?- write('Initial Theme: '), isDark(D), write(D), nl, toggle_theme, isDark(D1), write('Toggled Theme: '), write(D1), nl.
Toggles theme multiple times.
Frequently Asked Questions about Prolog
What is Prolog?
Prolog (Programming in Logic) is a high-level, declarative programming language focused on logic programming and symbolic reasoning. It is widely used in artificial intelligence, natural language processing, and rule-based systems, enabling developers to express knowledge and relationships rather than step-by-step instructions.
What are the primary use cases for Prolog?
Expert systems and rule-based AI. Natural language processing. Automated theorem proving. Knowledge representation and reasoning. Constraint logic programming
What are the strengths of Prolog?
Concise expression of complex logic. Ideal for symbolic reasoning. Automatic search and inference. Supports rapid prototyping of AI systems. Good for teaching logic programming concepts
What are the limitations of Prolog?
Not ideal for numerical computation or low-level tasks. Performance can degrade on large datasets. Less mainstream than procedural or object-oriented languages. Debugging can be challenging due to implicit control flow. Limited standard library for modern applications
How can I practice Prolog typing speed?
CodeSpeedTest offers 10+ real Prolog code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.