Prolog Programs with Output
Sudeep Kumar Singh, 22/28021
BSC Computer Science Hons.
Prolog Programs with Output
Name: Sudeep Kumar Singh
Roll No: 22/28021
Course: BSC Computer Science Hons.
Submitted to: Dr. Parul Jain Ma’am
Date: $(date)
1_family_tree
Code
% Facts
parent(john, mary).
parent(mary, susan).
parent(john, peter).
parent(peter, david).
% Rules
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
sibling(X, Y) :- parent(Z, X), parent(Z, Y), X \= Y.
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
% Demonstration queries:
% ?- grandparent(john, susan).
% ?- sibling(mary, peter).
% ?- ancestor(john, david).
Output
Query: grandparent(john, susan)
Query: sibling(mary, peter)
Query: ancestor(john, david)
1
2_concatenate_lists Submitted to Dr. Parul Jain Ma’am
PROLOG PROGRAMS WITH OUTPUT
2_concatenate_lists
Code
conc([], L, L).
conc([H|T], L2, [H|L3]) :- conc(T, L2, L3).
% Demonstration queries:
% ?- conc([1, 2], [3, 4], L3).
Output
Query: conc([1, 2], [3, 4], L)
[1,2,3,4]
3_reverse_list
Code
reverse(L, R) :- reverse_acc(L, [], R).
reverse_acc([], Acc, Acc).
reverse_acc([H|T], Acc, R) :- reverse_acc(T, [H|Acc], R).
% Demonstration queries:
% ?- reverse([1, 2, 3], R).
Output
Query: reverse([1, 2, 3], R)
[3,2,1]
4_sum_two_numbers
Code
sum(X, Y, Z) :- Z is X + Y.
% Demonstration queries:
% ?- sum(3, 4, Z).
Output
Query: sum(3, 4, Z)
7
5_maximum_two_numbers
Code
max(X, Y, X) :- X >= Y.
max(X, Y, Y) :- Y > X.
2
6_factorial Submitted to Dr. Parul Jain Ma’am
PROLOG PROGRAMS WITH OUTPUT
% Demonstration queries:
% ?- max(3, 4, M).
Output
Query: max(3, 4, M)
4
Query: max(7, 2, M)
7
6_factorial
Code
factorial(0, 1).
factorial(N, F) :-
N > 0,
N1 is N - 1,
factorial(N1, F1),
F is N * F1.
% Demonstration queries:
% ?- factorial(5, F).
Output
Query: factorial(5, F)
120
7_fibonacci_series
Code
generate_fib(0, 0).
generate_fib(1, 1).
generate_fib(N, T) :-
N > 1,
N1 is N - 1,
N2 is N - 2,
generate_fib(N1, T1),
generate_fib(N2, T2),
T is T1 + T2.
% Demonstration queries:
% ?- generate_fib(5, T).
Output
Query: generate_fib(5, T)
5
3
8_power Submitted to Dr. Parul Jain Ma’am
PROLOG PROGRAMS WITH OUTPUT
8_power
Code
power(_, 0, 1).
power(Num, Pow, Ans) :-
Pow > 0,
Pow1 is Pow - 1,
power(Num, Pow1, Ans1),
Ans is Num * Ans1.
% Demonstration queries:
% ?- power(2, 3, Ans).
Output
Query: power(2, 3, Ans)
8
9_multiplication
Code
multi(N1, N2, R) :- R is N1 * N2.
% Demonstration queries:
% ?- multi(3, 4, R).
Output
Query: multi(3, 4, R)
12
10_membership
Code
memb(X, [X|_]).
memb(X, [_|T]) :- memb(X, T).
% Demonstration queries:
% ?- memb(3, [1, 2, 3]).
Output
Query: memb(3, [1, 2, 3])
Query: memb(5, [1, 2, 3])
4
11_sum_of_list Submitted to Dr. Parul Jain Ma’am
PROLOG PROGRAMS WITH OUTPUT
11_sum_of_list
Code
sumlist([], 0).
sumlist([H|T], S) :-
sumlist(T, S1),
S is H + S1.
% Demonstration queries:
% ?- sumlist([1, 2, 3], S).
Output
Query: sumlist([1, 2, 3], S)
6
12_even_odd_length
Code
evenlength([]).
evenlength([_|T]) :- oddlength(T).
oddlength([_]).
oddlength([_|T]) :- evenlength(T).
% Demonstration queries:
% ?- evenlength([1, 2, 3, 4]).
% ?- oddlength([1, 2, 3]).
Output
Query: evenlength([1, 2, 3, 4])
Query: oddlength([1, 2, 3])
13_maximum_in_list
Code
maxlist([X], X).
maxlist([H|T], M) :-
maxlist(T, M1),
M is max(H, M1).
% Demonstration queries:
% ?- maxlist([1, 5, 3, 9, 2], M).
Output
Query: maxlist([1, 5, 3, 9, 2], M)
9
5
14_insert_element Submitted to Dr. Parul Jain Ma’am
PROLOG PROGRAMS WITH OUTPUT
14_insert_element
Code
insert(I, 1, L, [I|L]).
insert(I, N, [H|T], [H|R]) :-
N > 1,
N1 is N - 1,
insert(I, N1, T, R).
% Demonstration queries:
% ?- insert(a, 3, [1, 2, 3, 4], R).
Output
Query: insert(a, 3, [1, 2, 3, 4], R)
[1,2,a,3,4]
15_delete_element
Code
delete(1, [_|T], T).
delete(N, [H|T], [H|R]) :-
N > 1,
N1 is N - 1,
delete(N1, T, R).
% Demonstration queries:
% ?- delete(3, [1, 2, 3, 4], R).
Output
Query: delete(3, [1, 2, 3, 4], R)
[1,2,4]