Chapter Three Lists Operators Arithmetic CS 461 3

  • Slides: 14
Download presentation
Chapter Three: Lists, Operators, Arithmetic CS 461

Chapter Three: Lists, Operators, Arithmetic CS 461

3. 2 Some operations on lists 1. Concatenation 2. Adding an item 3. Deleting

3. 2 Some operations on lists 1. Concatenation 2. Adding an item 3. Deleting an item

Concatenation conc(L 1, L 2, L 3) L 1 and L 2 are two

Concatenation conc(L 1, L 2, L 3) L 1 and L 2 are two lists and L 3 is their concatenation. conc([a, b], [c, d], [a, b, c, d]) True conc([a, b], [c, d], [a, b, a, c, d]) False

Concatenation (cont. ) Definition of conc: (1) If the first argument is empty, empty

Concatenation (cont. ) Definition of conc: (1) If the first argument is empty, empty then the second and third arguments must be the same list conc([], L, L). (2) If the first argument of conc is non-empty list, list then it has a head and tail and must look like this [X|L 1] the result of concatenation of L 1 and L 2 is the list [X|L 3] where L 3 is the concatenation of L 1 and L 2 conc([X|L 1], L 2, [X|L 3]): - conc(L 1, L 2, L 3).

5 Concatenation (Example) conc([], L, L). conc([X|L 1], L 2, [X|L 3]): - conc(L

5 Concatenation (Example) conc([], L, L). conc([X|L 1], L 2, [X|L 3]): - conc(L 1, L 2, L 3). ? - conc([a, b, c], [1, 2, 3], L) L= [a, b, c, 1, 2, 3] ? -conc([a, [b, c], d], [a, [], b], L) L = [a, [b, c], d, a, [], b].

6 Concatenation (Example) conc([], L, L). conc([X|L 1], L 2, [X|L 3]): - conc(L

6 Concatenation (Example) conc([], L, L). conc([X|L 1], L 2, [X|L 3]): - conc(L 1, L 2, L 3). ? - conc(L 1, L 2, [a, b, c]) L 1 = [], L 2 = [a, b, c] ; L 1 = [a], L 2 = [b, c] ; L 1 = [a, b], L 2 = [c] ; L 1 = [a, b, c], L 2 = [] ; (decompose)

7 Concatenation (Example) How can we find the months that precedes and follows a

7 Concatenation (Example) How can we find the months that precedes and follows a given month in a list ? conc([], L, L). conc([X|L 1], L 2, [X|L 3]): - conc(L 1, L 2, L 3). ? -conc(Before, [may|After], [jan, feb, mar, apr, may, jun, jul, aug, sep, oct, n ov, dec]). Before = [jan, feb, mar, apr], After = [jun, jul, aug, sep, oct, nov, dec]

Class exercise (1) Write a goal , using conc , to delete the last

Class exercise (1) Write a goal , using conc , to delete the last three elements from the list L producing another list L 1. HINT : L is a concatenating of L 1 and another three elements list conc([], L, L). conc([X|L 1], L 2, [X|L 3]): - conc(L 1, L 2, L 3).

Answer: ? - L = [1, 2, 3, 4, 5], conc(L 1, [_, _,

Answer: ? - L = [1, 2, 3, 4, 5], conc(L 1, [_, _, _], L). L = [1, 2, 3, 4, 5], L 1 = [1, 2]

10 Concatenation Definition of member using conc: We can program the membership relation using

10 Concatenation Definition of member using conc: We can program the membership relation using conc: member 1(X, L): - conc(L 1, [X|L 2], L). X is a member of list L if L can be decopmosed into two lists so that the second one has X as its head. member 1(X, L): - conc(_, [X|_], L). member 1(X, L): - conc(L 1, [X|L 2], L). member 1(X, L): - conc(_, [X|_], L). ? - member 1( b, [a, b, c]).

11 Adding an item Simply to put an item in front of a list:

11 Adding an item Simply to put an item in front of a list: [X|L] Definition of add: The procedure can be written explicitly as the fact: add(X, L, [X, L]). Example ? -add(z, [a, b, c], New. List).

Delete an item • Deleting an item X from list L, can be programmed

Delete an item • Deleting an item X from list L, can be programmed as a relation: Del(X, L, L 1). Where L 1 is equal to L with the item X removed.

Delete an item • The del definition : We have two cases : 1)

Delete an item • The del definition : We have two cases : 1) If X is the head of the list, then the result after deletion will be the tail of the list. del(X, [X|Tail] , Tail). 2) If X is in the tail then it is deleted from there: del(X, [X|Tail] , Tail). del(X, [Y|Tail] , Tail 1) : - del(X, Tail 1).

14 Deletion (Example) del(X, [X|Tail] , Tail). del(X, [Y|Tail] , Tail 1) : -

14 Deletion (Example) del(X, [X|Tail] , Tail). del(X, [Y|Tail] , Tail 1) : - del(X, Tail 1). ? - del(a, [a, b, a, a], L). L = [b, a, a] ; L = [] ;