TDPL ASSIGNMENT-5
PROLOG PROGRAMMING
Name:Haswanth
Roll no:422238
Section : B
1. print all elements of a list ?-print_list([a,b,c]). a b c
print_list([]):-nl. %nl = newline
print_list([H|T]):-write(H),write(' '),print_list(T).
CODE:
% Base case: if the list is empty, print a newline
print_list([]) :- nl.
% Recursive case: print the head of the list and then recursively print the tail
print_list([H|T]) :-
write(H),
write(' '), % write a space after each element
print_list(T).
OUTPUT:
2. reverse all elements of a list ?-reversex([a,b,c],X). X=[c,b,a]
addtoend(H,[],[H]).
addtoend(X,[H|T],[H|T1]):-addtoend(X,T,T1).
reversex([],[]).
reversex([H|T],Y):- reversex(T,T1), addtoend(H,T1,Y).
CODE:
% addtoend(Element, List, Result): Adds an element to the end of the list.
addtoend(H, [], [H]).
addtoend(X, [H|T], [H|T1]) :- addtoend(X, T, T1).
% reversex(List, ReversedList): Reverses a list.
reversex([], []).
reversex([H|T], Y) :-
reversex(T, T1), % Recursively reverse the tail of the list.
addtoend(H, T1, Y). % Add the head element to the end of the reversed tail.
OUTPUT:
3. create list ?-create_list(5,12,S). S=[5,6,7,8,9,10,11,12]
create_list(X,X,[X]).
create_list(A,X,[A|T]):- AA is A+1, create_list(AA,X,T).
CODE:
% Base case: When A is equal to X, the list contains only X.
create_list(X, X, [X]).
% Recursive case: Add A to the list and increment A until A equals X.
create_list(A, X, [A|T]) :-
A < X, % Ensure that A is less than X before incrementing.
AA is A + 1, % Increment A.
create_list(AA, X, T). % Recursively call create_list with the incremented value.
OUTPUT:
4. mean value [1,2,3,4,5] => 3
sum_list([],0,0).
sum_list([H|T],Length,Sum):-sum_list(T,L1,S1), Length is L1+1, Sum is S1+H.
mean(L,M):-sum_list(L,Length,Sum), M is Sum/Length.
CODE:
% Base case: An empty list has a length and sum of 0.
sum_list([], 0, 0).
% Recursive case: Decompose the list into head (H) and tail (T),
% and recursively compute the length and sum.
sum_list([H|T], Length, Sum) :-
sum_list(T, L1, S1), % Recursively sum the tail.
Length is L1 + 1, % Increment the length.
Sum is S1 + H. % Add the head to the sum.
% mean(L, M) computes the mean of list L.
mean(L, M) :-
sum_list(L, Length, Sum), % Get the length and sum of the list.
Length > 0, % Ensure the list is non-empty.
M is Sum / Length. % Compute the mean.
OUTPUT:
5. detect whether list contains a number [a,b,c,d,e,1,f] => T
numberinlist([]):-fail.
numberinlist([X|T]):-number(X).
numberinlist([X|T]):-numberinlist(T).
CODE:
% Base case: Fails if the list is empty (no number found).
numberinlist([]) :- fail.
% If the head of the list is a number, succeed.
numberinlist([X|_]) :- number(X), !.
% If the head is not a number, recursively check the tail.
numberinlist([_|T]) :- numberinlist(T).
OUTPUT:
6. increment elements of list [5,6,7,8] => [6,7,8,9]
increment([],[]).
increment([H|T],[X|Y]):-increment(T,Y),X is H+1.
CODE:
% Base case: The empty list increments to an empty list.
increment([], []).
% Recursive case: Increment the head (H) of the list, and recursively process the tail (T).
increment([H|T], [X|Y]) :-
X is H + 1, % Increment the head.
increment(T, Y). % Recursively increment the tail.
OUTPUT:
7. factorial function
factorial(0,1).
factorial(N,X):-N>0, N1 is N-1, factorial(N1,S), X is S*N.
CODE:
% Base case: The factorial of 0 is 1.
factorial(0, 1).
% Recursive case: Calculate factorial for positive integers.
factorial(N, X) :-
N > 0, % Ensure N is greater than 0.
N1 is N - 1, % Calculate N-1.
factorial(N1, S), % Recursively calculate factorial of N-1.
X is S * N. % Calculate factorial of N.
OUTPUT:
8. implement append function [a,1,2,b,c], [b,c,d,e] => [a,1,2,b,c,b,c,d,e]
appendx([],A,A).
appendx([H|T],A,[H|U]):-appendx(T,A,U).
CODE:
% Base case: Appending an empty list to another list results in the second list.
appendx([], A, A).
% Recursive case: Decompose the first list into head (H) and tail (T).
appendx([H|T], A, [H|U]) :-
appendx(T, A, U). % Recursively append the tail (T) to list (A).
OUTPUT:
9. encapsulate list elements [a,b,1,d,e] => [[a],[b],[1],[d],[e]]
encapsulate([],[]).
encapsulate([H|T],[[H]|Y]):-encapsulate(T,Y).
CODE:
% Base case: An empty list becomes an empty list of lists.
encapsulate([], []).
% Recursive case: Wrap the head (H) of the list in a sublist and process the tail (T).
encapsulate([H|T], [[H]|Y]) :-
encapsulate(T, Y). % Recursively encapsulate the tail.
OUTPUT:
10. insert zeros [1,2,3,4,5] => [1,0,2,0,3,0,4,0,5,0]
insert_zeros([],[]).
insert_zeros([H|T],[H,0|Y]):-insert_zeros(T,Y).
CODE:
% Base case: An empty list becomes an empty list.
insert_zeros([], []).
% Recursive case: For a non-empty list, insert the head (H) and a zero (0).
insert_zeros([H|T], [H, 0 | Y]) :-
insert_zeros(T, Y). % Recursively process the tail (T).
OUTPUT:
11. clone list [g,6,7] => [[g,6,7][g,6,7]]
clone_list(T,[T,T]).
CODE:
% Base case: Cloning an empty list results in a list containing an empty list.
clone_list([], [[]]).
% Recursive case: Clone the head and the tail of the list.
clone_list(L, [L, L]).
OUTPUT:
12. modify list element ?-modify_list([m,o,d,i,f,y,e,t], 6, i,Y). Y=[m,o,d,i,f,y,i,t], ?-
modify_list([m,o,d],6,i,Y).
Y=[m,o,d]
modify_list([],N,X,[]).
modify_list([H|T],0,X,[X|T]).
modify_list([H|T],N,X,[H|Y]):-N>0, N1 is N-1, modify_list(T,N1,X,Y).
CODE:
% Base case: If the input list is empty, the result is also an empty list.
modify_list([], _, _, []).
% If the index is 0, replace the head of the list with the new element (X).
modify_list([_|T], 0, X, [X|T]). % Use _ to indicate we don't care about the head.
% Recursive case: Decrement the index and continue processing the tail.
modify_list([H|T], N, X, [H|Y]) :-
N > 0, % Ensure that N is positive.
N1 is N - 1, % Decrement N by 1.
modify_list(T, N1, X, Y). % Continue modifying the tail.
OUTPUT:
13.rotate list [1,2,3,4,5] => [2,3,4,5,1]
addtoend(H,[],[H]).
addtoend(X,[H|T],[H|T1]):-addtoend(X,T,T1).
rotate_list([H|T],L1):-addtoend(H,T,L1).
CODE:
% Base case: Adding an element to an empty list results in a list with that element.
addtoend(H, [], [H]).
% Recursive case for adding an element to the end of a non-empty list.
addtoend(X, [H|T], [H|T1]) :-
addtoend(X, T, T1).
% Rotate the list by moving the head (H) to the end.
rotate_list([H|T], L1) :-
addtoend(H, T, L1). % Append the head to the end of the tail.
OUTPUT:
14. Fibonacci numbers: print nth Fibonacci number.
fib(1,1).
fib(2,1).
fib(N,F):- N>2,
N1 is N-1, fib(N1,F1),
N2 is N-2, fib(N2,F2),
F is F1+F2.
CODE:
% Base cases: The first and second Fibonacci numbers are both 1.
fib(1, 1).
fib(2, 1).
% Recursive case: Calculate the Fibonacci number for N > 2.
fib(N, F) :-
N > 2, % Ensure N is greater than 2.
N1 is N - 1, % Calculate the (N-1)th Fibonacci number.
N2 is N - 2, % Calculate the (N-2)th Fibonacci number.
fib(N1, F1), % Recursive call for (N-1).
fib(N2, F2), % Recursive call for (N-2).
F is F1 + F2. % Sum the two previous Fibonacci numbers.
OUTPUT:
15. generate random (values 0-9) square matrix: [[2,4,5],[1,0,3],[9,3,2]]. Inner elements represent
matrix rows.
random10(N):-frandom(X), Y is X*10, fix(Y,N).
rand_row(0,[]).
rand_row(N,[H|T]):-N>0,random10(H),N1 is N-1, rand_row(N1,T).
square_matrix_rand(N,S):-smr(N,N,S).
smr(N,0,[]).
smr(N,X,[R|T]):-N>0, rand_row(N,R),X1 is X-1,smr(N,X1,T).
CODE:
% Generate a random number between 0 and 9
random10(X) :-
random(0, 10, X). % Generates a random integer between 0 (inclusive) and 10 (exclusive)
% Generate a row of random values
rand_row(0, []). % Base case: if N is 0, the row is empty
rand_row(N, [H|T]) :-
N > 0,
random10(H), % Generate a random number for the head
N1 is N - 1, % Decrement N
rand_row(N1, T). % Recursive call to generate the tail
% Generate a square matrix of size N
square_matrix_rand(N, S) :-
square_matrix_rand(N, N, S). % Call the helper predicate
% Helper predicate to create the matrix
square_matrix_rand(_, 0, []). % Base case: if rows remaining is 0, the matrix is empty
square_matrix_rand(N, RowsLeft, [R|T]) :-
RowsLeft > 0,
rand_row(N, R), % Generate a random row of length N
RowsLeft1 is RowsLeft - 1, % Decrement rows remaining
square_matrix_rand(N, RowsLeft1, T). % Recursive call to generate the next row
OUTPUT:
16. member ?-memberx(2,[1,2,3]). yes, ?-memberx([1,2,a],[1,2,3,[1,2,a]]). yes, ?-
memberx(4,[1,2,3,[1,2,a]]). no
memberx(N,[N|T]).
memberx(N,[X|T]):-memberx(N,T).
CODE:
% Base case: N is the head of the list
memberx(N, [N|_]). % If N is the first element, it's a member.
% Recursive case: N is in the tail of the list
memberx(N, [_|T]) :-
memberx(N, T). % Continue searching in the tail.
% Handle the case where the head is a list
memberx(N, [H|_]) :-
is_list(H), % Check if the head is a list
memberx(N, H). % Check for membership in the nested list.
% To make sure N is not found in an empty list
memberx(_, []). % Membership check fails for an empty list.
OUTPUT:
17. implement insertion into a sorted list (the result is sorted as well)
insertinto(N,[],[N]).
insertinto(N,[H|T],[N,H|T]):-H>=N,!.
insertinto(N,[H|T],[H|Y]):-insertinto(N,T,Y).
CODE:
% Base case: Inserting into an empty list
insertinto(N, [], [N]). % If the list is empty, the result is a list with N.
% Case 1: Insert N before the head H if N is less than or equal to H
insertinto(N, [H|T], [N, H|T]) :-
H >= N, !. % If H is greater than or equal to N, insert N before H and cut.
% Case 2: Recursive call: Insert N in the tail of the list
insertinto(N, [H|T], [H|Y]) :-
insertinto(N, T, Y). % Continue searching in the tail.
OUTPUT:
18. search duplicates. Result don't contain duplicate elements. [a,b,1,c,3,d,2,2,f,3] => [3,2]
memberx(N,[N|T]).
memberx(N,[X|T]):-memberx(N,T).
deleteall(N,[],[]).
deleteall(N,[N|T],U):-!,deleteall(N,T,U).
deleteall(N,[H|T],[H|U]):-deleteall(N,T,U).
delete_dupl([],[]).
delete_dupl([H|T],Y):-memberx(H,T),!,deleteall(H,T,T1),delete_dupl(T1,Y).
delete_dupl([H|T],[H|Y]):-delete_dupl(T,Y).
CODE:
% Check if an element is a member of a list
memberx(N, [N|_]). % N is the head of the list.
memberx(N, [_|T]) :- memberx(N, T). % Recur through the tail.
% Delete all occurrences of N from a list
deleteall(_, [], []). % Base case: deleting from an empty list yields an empty list.
deleteall(N, [N|T], U) :-
!, deleteall(N, T, U). % If N is the head, skip it and continue deleting in the tail.
deleteall(N, [H|T], [H|U]) :-
deleteall(N, T, U). % Keep H and recur on the tail.
% Find duplicates in a list
delete_dupl([], []). % Base case: an empty list has no duplicates.
delete_dupl([H|T], [H|Duplicates]) :-
memberx(H, T), % If H is found in the tail T,
deleteall(H, T, T1), % remove all occurrences of H from T.
delete_dupl(T1, Duplicates). % Find duplicates in the modified tail.
delete_dupl([H|T], Duplicates) :-
\+ memberx(H, T), % If H is not a member of T,
delete_dupl(T, Duplicates). % Continue checking the tail.
OUTPUT:
19. remove unique elements [6,2,3,3,5,2,3,1,4] => [2,3,3,2,3]
memberx(N,[N|T]).
memberx(N,[X|T]):-memberx(N,T).
delete_unique([],[]).
delete_unique([H|T],[H|Y]):-memberx(H,T),!,delete_unique(T,Y).
delete_unique([H|T],Y):-delete_unique(T,Y).
CODE:
% Check if an element is a member of a list
memberx(N, [N|_]). % N is the head of the list.
memberx(N, [_|T]) :- memberx(N, T). % Recur through the tail.
% Remove unique elements from a list
delete_unique([], []). % Base case: an empty list has no unique elements.
delete_unique([H|T], [H|Y]) :-
memberx(H, T), % If H is a member in T, it's not unique.
!, delete_unique(T, Y). % Keep H and continue with T.
delete_unique([H|T], Y) :-
\+ memberx(H, T), % If H is not a member of T, it's unique.
delete_unique(T, Y). % Skip H and continue with T.
OUTPUT:
20. make a list unique [a,b,c,d,a,b,e,f] => [a,b,c,d,e,f]
memberx(N,[N|T]).
memberx(N,[X|T]):-memberx(N,T).
deleteall(N,[],[]).
deleteall(N,[N|T],U):-!,deleteall(N,T,U).
deleteall(N,[H|T],[H|U]):-deleteall(N,T,U).
make_unique([],[]).
make_unique([H|T],[H|Y]):-memberx(H,T),!,deleteall(H,T,T1),make_unique(T1,Y).
make_unique([H|T],[H|Y]):-make_unique(T,Y).
or [a,b,c,d,a,b,e,f] => [c,d,a,b,e,f]
CODE:
% Check if an element is a member of a list
memberx(N, [N|_]). % N is the head of the list.
memberx(N, [_|T]) :- memberx(N, T). % Recur through the tail.
% Delete all occurrences of N from a list
deleteall(_, [], []). % Base case: deleting from an empty list results in an empty list.
deleteall(N, [N|T], U) :-
!, deleteall(N, T, U). % Skip N and continue with the tail.
deleteall(N, [H|T], [H|U]) :-
deleteall(N, T, U). % Keep H and continue with the tail.
% Make a list unique
make_unique([], []). % Base case: an empty list is unique.
make_unique([H|T], [H|Y]) :-
memberx(H, T),
!, deleteall(H, T, T1),
make_unique(T1, Y). % If H is a member in T, delete all occurrences of H and continue.
make_unique([H|T], [H|Y]) :-
make_unique(T, Y). % If H is not a member in T, just continue.
OUTPUT:
21. Define a predicate quicksort(L,K) which, given a list of
integers L, returns an ordered list K obtained from L with the
method of quicksort.
CODE:
% Base case: An empty list is already sorted.
quicksort([], []).
% Recursive case:
quicksort([Pivot|Rest], Sorted) :-
partition(Rest, Pivot, Less, Greater),
quicksort(Less, SortedLess), % Recursively sort the less than pivot list
quicksort(Greater, SortedGreater), % Recursively sort the greater than pivot list
append(SortedLess, [Pivot|SortedGreater], Sorted). % Combine the results
% Partition the list into two parts: elements less than the pivot and those greater.
partition([], _, [], []). % Base case: empty list returns two empty lists.
partition([H|T], Pivot, [H|Less], Greater) :-
H < Pivot,
partition(T, Pivot, Less, Greater). % If H is less than the pivot, add to Less.
partition([H|T], Pivot, Less, [H|Greater]) :-
H >= Pivot,
partition(T, Pivot, Less, Greater). % If H is greater than or equal to the pivot, add to Greater.
OUTPUT:
22. Define a predicate merge(L,K,M) which, given two ordered lists
of integers L and K, returns an ordered list M containing all the
elements of L and K.
CODE:
% Base case: If L is empty, M is just K.
merge([], K, K).
% Base case: If K is empty, M is just L.
merge(L, [], L).
% Recursive case: If the head of L is smaller than or equal to the head of K,
% add the head of L to the result list and continue merging the tail of L with K.
merge([H1|T1], [H2|T2], [H1|M]) :-
H1 =< H2,
merge(T1, [H2|T2], M).
% Recursive case: If the head of K is smaller than the head of L,
% add the head of K to the result list and continue merging L with the tail of K.
merge([H1|T1], [H2|T2], [H2|M]) :-
H1 > H2,
merge([H1|T1], T2, M).
OUTPUT:
23. Define a predicate add_up_list(L,K) which, given a list of integers
L, returns a list of integers in which each element is the sum of all
the elements in L up to the same position. Example:
?- add_up_list([1,2,3,4],K).
K = [1,3,6,10];
No
CODE:
% Entry point for adding up the list.
add_up_list(L, K) :-
add_up_list_helper(L, 0, K).
% Base case: The cumulative sum of an empty list is an empty list.
add_up_list_helper([], _, []).
% Recursive case: Calculate the cumulative sum.
add_up_list_helper([H|T], Acc, [NewAcc|K]) :-
NewAcc is Acc + H, % Calculate the new accumulated sum.
add_up_list_helper(T, NewAcc, K). % Recurse over the tail.
OUTPUT:
24. Define a predicate sumlist(L,N) which, given a list of integers L,
returns the sum N of all the elements of L.
CODE:
% Base case: The sum of an empty list is 0.
sumlist([], 0).
% Recursive case: Sum the head and the sum of the tail.
sumlist([H|T], N) :-
sumlist(T, SumTail), % Recursively get the sum of the tail.
N is H + SumTail. % Calculate the total sum.
OUTPUT:
25. Define a predicate occurs(L,N,X) which holds iff X is the element
occurring in position N of the list L.
CODE:
% Base case: If N is 1, X is the head of the list.
occurs([X|_], 1, X).
% Recursive case: Decrement N and look for X in the tail.
occurs([_|T], N, X) :-
N > 1, % Ensure N is greater than 1.
N1 is N - 1, % Decrement N.
occurs(T, N1, X). % Recursive call on the tail.
OUTPUT:
26. Define a predicate length(L,N) which holds iff N is the length of
the list L.
CODE:
% Base case: The length of an empty list is 0.
my_length([], 0).
% Recursive case: The length of a non-empty list is 1 plus the length of the tail.
my_length([_|T], N) :-
my_length(T, N1), % Recursively find the length of the tail.
N is N1 + 1. % Add 1 for the head.
OUTPUT:
27. Define a predicate occurrences(X,L,N) which holds iff the
element X occurs N times in the list L.
CODE:
% Base case: If the list is empty, X occurs 0 times.
occurrences(_, [], 0).
% Recursive case: If the head of the list matches X, count it and continue.
occurrences(X, [X|T], N) :-
occurrences(X, T, N1), % Count occurrences in the tail.
N is N1 + 1. % Increment the count.
% Recursive case: If the head of the list does not match X, just continue.
occurrences(X, [Y|T], N) :-
X \= Y, % Ensure X does not match Y.
occurrences(X, T, N). % Count occurrences in the tail.
OUTPUT:
28. Define a predicate reverse(L,K) which holds if and only if the list
K is the reverse of the list L.
CODE:
% Base case: The reverse of an empty list is an empty list.
reverse([], []).
% Recursive case: Reverse the tail and append the head to the end of the reversed tail.
reverse([H|T], R) :-
reverse(T, RevT), % Reverse the tail.
append(RevT, [H], R). % Append the head to the reversed tail.
OUTPUT:
29. Write a predicate, nth(N, TheList, TheItem), which is true if
TheItem is the N’th item in TheList. Counting begins at one.
nth(1,Alist,Elem) is true for the first item in the list.
CODE:
% Base case: The first item (N=1) in TheList is TheItem.
nth(1, [TheItem|_], TheItem).
% Recursive case: Decrease N and look for TheItem in the tail of TheList.
nth(N, [_|Tail], TheItem) :-
N > 1, % Ensure N is greater than 1.
N1 is N - 1, % Decrement N.
nth(N1, Tail, TheItem). % Recursive call on the tail.
OUTPUT:
30. Write simple Prolog functions such as the following. Take into
account lists which are too short.
-- remove the N’th item from a list.
-- insert as the N’th item.
CODE:
% 1. Remove the N'th item from a list
remove_nth(1, [_|Tail], Tail). % If N is 1, remove the first item
remove_nth(N, [Head|Tail], [Head|ResultTail]) :- % If N > 1, recurse
N > 1,
N1 is N - 1,
remove_nth(N1, Tail, ResultTail).
remove_nth(N, [], none) :- N > 0. % Return 'none' if the list is too short
% 2. Insert an item as the N'th item in a list
insert_nth(Item, 1, List, [Item|List]). % Insert at the beginning
insert_nth(Item, N, [Head|Tail], [Head|ResultTail]) :- % Recurse to find position
N > 1,
N1 is N - 1,
insert_nth(Item, N1, Tail, ResultTail).
insert_nth(_, N, [], none) :- N > 0. % Return 'none' if the list is too short
OUTPUT:
31. reverse all elements of a list ?-reversex([a,b,c],X). X=[c,b,a]
CODE:
% Base case: the reverse of an empty list is an empty list
reversex([], []).
% Recursive case: reverse the tail and append the head to the end
reversex([Head|Tail], Reversed) :-
reversex(Tail, ReversedTail),
append(ReversedTail, [Head], Reversed).
OUTPUT:
32. detect whether list contains a number [a,b,c,d,e,1,f] => T
CODE:
% Base case: An empty list does not contain a number.
contains_number([]) :-
false.
% Recursive case: If the head is a number, succeed.
contains_number([Head|_]) :-
number(Head),
!. % Cut to prevent backtracking.
% Recursive case: Continue searching in the tail.
contains_number([_|Tail]) :-
contains_number(Tail).
OUTPUT:
33. increment elements of list [5,6,7,8] => [6,7,8,9]
CODE:
% Base case: Incrementing an empty list results in an empty list.
increment_list([], []).
% Recursive case: Increment the head and recursively increment the tail.
increment_list([Head|Tail], [NewHead|NewTail]) :-
NewHead is Head + 1, % Increment the head
increment_list(Tail, NewTail). % Recur for the tail
OUTPUT:
34. factorial function
CODE:
% Predicate to calculate the factorial of a number
factorial(0, 1). % Base case: 0! = 1
factorial(N, Result) :-
N > 0, % Ensure N is positive
N1 is N - 1, % Calculate N-1
factorial(N1, Result1), % Recur to find (N-1)!
Result is N * Result1. % Calculate N!
OUTPUT:
35. implement append function [a,1,2,b,c], [b,c,d,e] =>
[a,1,2,b,c,b,c,d,e]
CODE:
% Predicate to append two lists
append([], L, L). % Base case
append([H|T], L2, [H|Result]) :- % Recursive case
append(T, L2, Result). % Append the tail and construct the result
OUTPUT:
36.encapsulate list elements [a,b,1,d,e] => [[a],[b],[1],[d],[e]]
CODE:
% Predicate to encapsulate list elements
encapsulate([], []). % Base case
encapsulate([H|T], [[H]|EncapsulatedTail]) :- % Recursive case
encapsulate(T, EncapsulatedTail). % Encapsulate the tail
OUTPUT:
37. insert zeros [1,2,3,4,5] => [1,0,2,0,3,0,4,0,5,0]
CODE:
% Predicate to insert zeros between elements of a list
insert_zeros([], []). % Base case: Empty list results in empty list.
insert_zeros([H|T], [H, 0|ResultTail]) :- % Recursive case
insert_zeros(T, ResultTail). % Insert zero after the head and recurse.
% Optionally, ensure the last zero is added.
insert_zeros([], [0]). % This handles the case for the final zero if needed.
OUTPUT:
38. modify list element ?-modify_list([m,o,d,i,f,y,e,t], 6, i,Y).
Y=[m,o,d,i,f,y,i,t], ?-modify_list([m,o,d],6,i,Y). Y=[m,o,d]
CODE:
% Base case: If the index is less than 1, return the original list unchanged.
modify_list(List, N, _, List) :-
N < 1, !.
% Base case: If the index is 1, replace the head of the list.
modify_list([_|T], 1, NewElem, [NewElem|T]).
% Recursive case: Decrease the index and continue to modify the tail.
modify_list([H|T], N, NewElem, [H|NewTail]) :-
N > 1,
N1 is N - 1,
modify_list(T, N1, NewElem, NewTail).
% Handle cases where the index is greater than the list length.
modify_list(List, N, _, List) :-
length(List, L),
N > L. % Return the original list if N is greater than length
OUTPUT:
39.Fibonacci numbers: print nth Fibonacci number.
CODE:
% Base cases for Fibonacci sequence
fibonacci(0, 0). % The 0th Fibonacci number is 0
fibonacci(1, 1). % The 1st Fibonacci number is 1
% Recursive case: Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2)
fibonacci(N, F) :-
N > 1,
N1 is N - 1,
N2 is N - 2,
fibonacci(N1, F1),
fibonacci(N2, F2),
F is F1 + F2.
OUTPUT:
40. maximum function [1,-2,3] => 3
CODE:
% Base case: The maximum of a single-element list is that element.
max_element([X], X).
% Recursive case: Compare the head with the maximum of the tail.
max_element([H|T], Max) :-
max_element(T, MaxTail), % Find the maximum of the tail
Max is max(H, MaxTail). % Compare head and tail maximums
OUTPUT: