0% found this document useful (0 votes)
13 views6 pages

List Manipulation in Prolog

The document provides an overview of list manipulation in Prolog, detailing the structure of lists and basic operations such as checking for emptiness, accessing head and tail, and concatenating lists. It also covers common processing examples like finding elements, reversing lists, and calculating lengths, along with advanced techniques such as mapping, filtering, and aggregating values. Practical applications include sorting, flattening nested lists, and generating permutations.

Uploaded by

Mary ann Mora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

List Manipulation in Prolog

The document provides an overview of list manipulation in Prolog, detailing the structure of lists and basic operations such as checking for emptiness, accessing head and tail, and concatenating lists. It also covers common processing examples like finding elements, reversing lists, and calculating lengths, along with advanced techniques such as mapping, filtering, and aggregating values. Practical applications include sorting, flattening nested lists, and generating permutations.

Uploaded by

Mary ann Mora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

List Manipulation in Prolog

Applying List Processing in Prolog


Introduction to Lists in Prolog
• • A list is a fundamental data structure in
Prolog.
• • Represented using square brackets: [Head|
Tail].
• • Head is the first element, Tail is the
remainder.
• • Example: [1, 2, 3] or [X|Y].
Basic Operations on Lists
• • Check if a list is empty:
• Example: my_empty([]).

• • Access head and tail:


• Example: head_tail([H|T], H, T).

• • Concatenate lists:
• Example: append([1,2], [3], Result).
Common List Processing Examples
• • Find an element in a list:
• member(X, [1, 2, 3]).

• • Reverse a list:
• reverse([1, 2, 3], R).

• • Find the length of a list:


• length([1, 2, 3], L).
Advanced List Processing
• • Map elements to a new list:
• maplist(+Predicate, +List1, -List2).

• • Filter elements:
• include(+Predicate, +List1, -List2).

• • Aggregate values:
• foldl(+Predicate, +List, +Acc, -Result).
Practical Applications
• • Sorting a list:
• Example: sort([3, 1, 2], SortedList).

• • Flattening nested lists:


• Example: flatten([1, [2, 3], 4], FlatList).

• • Generating permutations:
• Example: permutation([1, 2, 3], P).

You might also like