0% found this document useful (0 votes)
36 views

List in Prolog

Prolog lists are defined using square brackets. An empty list is [ ] and a non-empty list is [Head|Tail] where the Head is the first item and Tail is the remaining items. Common list operations in Prolog include concatenation to join two lists, membership testing to check if an item is in a list, and deletion to remove an item from a list. Deletion is challenging as the item can be in the Head or Tail requiring different logic.

Uploaded by

MooN DiR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

List in Prolog

Prolog lists are defined using square brackets. An empty list is [ ] and a non-empty list is [Head|Tail] where the Head is the first item and Tail is the remaining items. Common list operations in Prolog include concatenation to join two lists, membership testing to check if an item is in a list, and deletion to remove an item from a list. Deletion is challenging as the item can be in the Head or Tail requiring different logic.

Uploaded by

MooN DiR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

List in prolog

In Prolog, you can define a list using square brackets [ ].

List([1, 2, 3, 4]).

An empty list can be defined as [ ],

A non-empty list can be defined as [Head | Tail]

The first item, called the Head of the list.

The remaining part of the list, called the Tail.

Example:

List([red, blue, green, yellow]).

Head = red

Tail = [blue, green, yellow]

Conter example:

Tail = [b, c, d, e].

L = ([ a | Tail ]).

Result : L = [a,b,c,d,e]
Operations

1. Concatination :

Concat ( [ ] , L , L).
Concat( [X1] | L1 , L2 , [X1 | L3] )
:- Concat ( L1 , L2 , L3) .
2. Membership
ListMember ( X , L )

Where : X : an object

L : is a List

Goal :

ListMember is True if X OCCURS in L

Observation:

X is a Member of L if:

1. X is the Head of the list OR


2. X is a member of the Tail

Code:

Listmember ( X , [ X | _ ] ).

Listmember ( X , [ _ | Tail] )

:- list member (X , Tail).


3. Delete

The item may be found in the Head or in the Tail

Cases:
1. If X is the Head of the list,
Result: The Tail of the List
2. If X is in the Tail,
Then, it is deleted from there

Code:

delete ( Y , [ Y ] , [ ] ).

delete( X , [ X | Tail ], Tail ).

delete( X , [ Y | List ] , [ Y | Tail ] )

:- delete ( X , List , Tail ).

PS : the delete function it is not working, try to solve it for the next TP

You might also like