0% found this document useful (0 votes)
2 views5 pages

Experiment 5

This document provides sample Prolog programs for list handling, including finding an element, replacing an element, and appending two lists. It includes code examples and explanations for each function, as well as instructions for saving and running the program in GNU Prolog. The document emphasizes the use of recursion and pattern matching in Prolog for list processing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Experiment 5

This document provides sample Prolog programs for list handling, including finding an element, replacing an element, and appending two lists. It includes code examples and explanations for each function, as well as instructions for saving and running the program in GNU Prolog. The document emphasizes the use of recursion and pattern matching in Prolog for list processing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Sample Programs using List Handling in Prolog

(a) Demonstrate the use of List handling in Prolog which include finding an element, replacing
an element

(b) Write a program to append two givsen list created in Prolog Programming.

% Finding an element in a list


find_element(Element, [Element|_]).
find_element(Element, [_|Rest]) :-
find_element(Element, Rest).

% Replacing an element in a list


replace_element(_, _, [], []).
replace_element(Old, New, [Old|T], [New|T2]) :-
replace_element(Old, New, T, T2).
replace_element(Old, New, [H|T], [H|T2]) :-
H \= Old,
replace_element(Old, New, T, T2).

% Appending two lists


append_lists([], L, L).
append_lists([H|T], L, [H|R]) :-
append_lists(T, L, R).

Save the file as list_operations.pl in a folder (e.g., C:\Prolog\).

Run the Program in GNU Prolog

1. Open Command Prompt (cmd) and navigate to the folder where the file is saved:

cd C:\Prolog\

2. Start GNU Prolog by typing:

gprolog

3. Load the Prolog file:

[list_operations].

4. Run queries to test:

% Check if 5 is in the list


?- find_element(5, [1,2,3,4,5]).

% Replace element 2 with 99 in the list


?- replace_element(2, 99, [1,2,3,4,2], X).

% Append two lists


?- append_lists([1,2,3], [4,5,6], X).

Expected Output:

X = [1,2,3,4,5,6].

Program: List Handling in Prolog


% Finding an element in a list
find_element(Element, [Element|_]).
find_element(Element, [_|Rest]) :-
find_element(Element, Rest).

Explanation

1. Base Case:

find_element(Element, [Element|_]).

o If the head of the list is the element we are looking for, return true.
o _ (underscore) represents the rest of the list, which we don't need to check
anymore.
o Example:

?- find_element(3, [1,2,3,4,5]).

Output: true (since 3 is present in the list).

2. Recursive Case:

find_element(Element, [_|Rest]) :-
find_element(Element, Rest).

o If the head is not the element, we ignore it (_) and check the rest (Rest).
o Recursively call find_element/2 on the rest of the list.
o Example:

?- find_element(7, [1,2,3,4,5]).

Output: false (since 7 is not in the list).

% Replacing an element in a list


replace_element(_, _, [], []).
replace_element(Old, New, [Old|T], [New|T2]) :-
replace_element(Old, New, T, T2).
replace_element(Old, New, [H|T], [H|T2]) :-
H \= Old,
replace_element(Old, New, T, T2).
Explanation

1. Base Case:

replace_element(_, _, [], []).

o If the list is empty, return an empty list ([]).


o Example:

?- replace_element(3, 99, [], X).

Output: X = [].

2. Replacing Occurrence of Old with New:

replace_element(Old, New, [Old|T], [New|T2]) :-


replace_element(Old, New, T, T2).

o If the head of the list is the element (Old) to be replaced:


 Replace it with New.
 Recursively process the tail (T).
o Example:

?- replace_element(3, 99, [1,3,5,3,7], X).

Output: X = [1,99,5,99,7].

3. If the Element is Different:

replace_element(Old, New, [H|T], [H|T2]) :-


H \= Old,
replace_element(Old, New, T, T2).

o If the head (H) is not the element we want to replace:


 Keep it unchanged.
 Recursively check the rest (T).
o Example:

?- replace_element(9, 99, [1,3,5,7], X).

Output: X = [1,3,5,7] (no changes).

% Appending two lists


append_lists([], L, L).
append_lists([H|T], L, [H|R]) :-
append_lists(T, L, R).
Explanation

1. Base Case:

append_lists([], L, L).

o If the first list is empty, return the second list (L).


o Example:

?- append_lists([], [4,5,6], X).

Output: X = [4,5,6].

2. Recursive Case:

append_lists([H|T], L, [H|R]) :-
append_lists(T, L, R).

o Take the head of the first list (H).


o Add it to the result list ([H|R]).
o Recursively append the rest (T) to L.
o Example:

?- append_lists([1,2,3], [4,5,6], X).

Output: X = [1,2,3,4,5,6].

Final Notes
✔ Recursive Approach:

 Each predicate works by checking base cases first.


 Then, it breaks down the list step by step using recursion.

✔ List Processing in Prolog:

 Prolog uses pattern matching ([H|T]) to work with lists.


 _ (underscore) is used when a value does not matter.

✔ How to Run the Program in GNU Prolog:

 Save the file as list_operations.pl.


 Open Command Prompt and navigate to the file’s location.
 Run:
[list_operations].

 Test queries:

?- find_element(3, [1,2,3,4,5]).
?- replace_element(2, 99, [1,2,3,4,2], X).
?- append_lists([1,2,3], [4,5,6], X).

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy