1 / 15

Chapter Three: Lists, Operators, Arithmetic

Chapter Three: Lists, Operators, Arithmetic. CS 461. 3.2 Some operations on lists. Concatenation Adding an item Deleting an item. Concatenation. conc (L1,L2,L3) L1 and L2 are two lists and L3 is their concatenation. conc([ a,b ],[ c,d ],[ a,b,c,d ]) True

oral
Download Presentation

Chapter Three: Lists, Operators, Arithmetic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter Three: Lists, Operators, Arithmetic CS 461

  2. 3.2 Some operations on lists • Concatenation • Adding an item • Deleting an item

  3. Concatenation conc(L1,L2,L3) L1 and L2 are two lists and L3 is their concatenation. conc([a,b],[c,d],[a,b,c,d]) True conc([a,b],[c,d],[a,b,a,c,d]) False

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

  5. Concatenation (Example) 2. • conc([],L,L). • conc([X|L1],L2,[X|L3]):- conc(L1,L2,L3). ?- 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) 2. • conc([],L,L). • conc([X|L1],L2,[X|L3]):- conc(L1,L2,L3). ?- conc(L1,L2,[a,b,c]) (decompose)

  7. Concatenation (Example) 2. • conc([],L,L). • conc([X|L1],L2,[X|L3]):- conc(L1,L2,L3). ?- conc(L1,L2,[a,b,c]) (decompose) L1 = [], L2 = [a, b, c] ; L1 = [a], L2 = [b, c] ; L1 = [a, b], L2 = [c] ; L1 = [a, b, c], L2 = [] ;

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

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

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

  11. Concatenation Definition of member using conc: We can program the membership relation using conc: member1(X,L):- conc(L1,[X|L2],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. member1(X,L):- conc(_,[X|_],L). member1(X,L):- conc(L1,[X|L2],L). member1(X,L):- conc(_,[X|_],L). ?- member1( b,[a,b,c]).

  12. 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],NewList).

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

  14. Delete an item • The del definition : We have two cases : • 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] , Tail1) :- del(X, Tail, Tail1).

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

More Related