Download presentation
Presentation is loading. Please wait.
Published byHorace Gordon Modified over 9 years ago
1
Lists in Prolog Sections 3.1, 3.2
2
Lists n List = sequence of values –[1, 2, 3, 4, 5] –[bob, brian, cathy, mark, david, loretta] –[birds(4, calling), hens(3, french), doves(2, turtle), partridge(1, in(tree(1,pear)))] –[X, Y, Z, Z, Y] –[1, brian, doves(2, turtle), Z]
3
Representations of Lists n Written in [square] brackets –Note: (parentheses), [brackets], {braces} –mean different things in computer languages n Elements, separated, by, commas n Each element can be anything –atom, term, variable, or list n Simplest list is the empty list: []
4
Head and Tail n First element of a list is called its head n Everything else in list is part of the tail –tail AKA “the rest of the list” –tail is itself a list n [1, b, 3, 5] –head is 1 – note: an atom (in this example) –tail is [b, 3, 5] – note: a list (always)
5
List Decapitation n Can separate the head from the tail n [Head | Tail] –use upright bar instead of comma ?- [1, b, 3, 5] = [Head | Tail]. Head = 1 Tail = [b, 3, 5] Yes
6
List Deconstruction n Can actually pull any number of elements off the front of the list ?- [1, 2, 3, 4, 5, 6] = [A, B, C | Tail]. A = 1 B = 2 C = 3 Tail = [4, 5, 6] No comma in front of the bar
7
Deconstruct Only At Front n Can’t get at the back elements (directly) ?- [1, 2, 3, 4, 5] = [Head | Tail, Last]. No n Only one item appears after the bar –it will become a list, if it isn’t already
8
Empty Tails n It’s OK if the tail is the empty list ?- [Head | Tail] = [alone]. Head = alone, Tail = [] ?- [A, B | Rest] = [1, 2]. A = 1, B = 2, Rest = [] ?- [A, B, C | More] = [1, 2]. No
9
Constructing Lists n Same way as you deconstruct them –only in reverse ?- _H = 1, _T = [2, 3, 4], L = [_H | _T]. L = [1, 2, 3, 4] Yes n Can use other list forms as well –[A, B, C, D] or [A, B, C | Tail], for example
10
Exercise n Show Prolog’s response… ?- [4, 3, 2, 1] = [H | T]. ?- [S, E, N, D | More] = [2, 4, 6, 8, 10]. ?- [T, h, I, s] = [t, H, a, T]. ?- [1, 2, 3 | More] = [X, Y, Z, Z, Y].
11
Lists in Terms n Lists can be arguments in terms –a([1, 2, 3]) –b(x, [w, e, r]) n And also any combinations thereof –[a([1, 2, 3]), [b(x, [w, e, r]), c], d, e]
12
Lists in Lists n A list can be an element of a list n [[a,b,c], [1,2,3], [do,re,mi]] has 3 elements –first element is the list [a, b, c] –second element is the list [1, 2, 3] –third/last element is the list [do, re, mi] n Can have lists in lists in lists as deep as you want – tho it can get hard to read –[a, [b], [[c,d], [e,f,g], [h,[[[[[i, j]]]]]]]
13
Built-In Predicates for Lists n Find the length of a list –length/2 n Ask if some element in a list –member/2 n Add one list to the end of another –append/3conc/3 in the text ?- apropos(list).% for more
14
The Length of a List n Want to know how long a list is ?- length([1, 2, 3, 4], N). N = 4 Yes n Only counts top-level elements ?- length([a, [b, c, d], e], N). N = 3 Yes
15
The length/2 Relationship n Can also generate list of desired length ?- length(L, 3). L = [_G263, _G269, _G272] Yes n length/2 is a relationship –between a list and a natural number –holds if the list has that many elements –not just for “counting elements”
16
Generating Every List n Can ask for any list of any length ?- length(L, N). L = [], N = 0 ; L = [_G1], N = 1 ; L = [_G1, _G2], N = 2 ; L = [_G1, _G2, _G3], N = 3 ; … n It’ll generate answers as long as you like
17
Printing Long Lists n Prolog may not print all the elements of a long list –[1, 2, 3, 4, 5, 6, 7, 8, 9|…] –Saves space – easier to read n If you want to see the whole thing: –type w instead of space or semi-colon –(semi-)permanent change; type p to go back –also applies when tracing
18
List Membership n We have some thing X and some list L n Want to know if X is in L ?- member(X, L). n Note: looks only at the top-level elements –member(b, [a, b, c]) succeeds –member(b, [a, [b, c], d]) fails –member(b, list(a, b, c, d)) fails
19
Taking Elements One at a Time n member/2 will, as usual, fill in variables as required to get true answers ?- member(X, [1, 2, 3]). X = 1 ; X = 2 ; X = 3 ; No member/2 is a relationship holds between X and L if X is in L
20
Filling In a List n Variables are instantiated in the list itself ?- member(3, [1, 2, X, 4]). X = 3 Yes ?- length(L, 2), member(1, L), member(2, L). L = [1, 2] ; L = [2, 1] Yes
21
member/2 Implemented in Prolog n X is in the list if: –it’s the head of the list, or –it’s in the tail of the list ?- listing(member). member(A, [A|B]). member(A, [B|C]) :- member(A, C). Recursive definition Common in predicates for lists
22
member/2 Clauses n Base case: –member(H, [H | _]). –first element of a list is a member of the list –[H | _] is H followed by anything (pattern) n Recursive case –member(E, [_ | T]) :- member(E, T). –member of tail of list is member of list
23
Exercise n Show (all) Prolog’s responses… ?- length([1, 2, [3, 4], [5, 6]], N). ?- length([1, 2, 3 | T], 4), member(5, T). ?- length(L, 3), member(a, L), member(b, L). ?- member(a, [[X, Y]]), member(b, [[X, Y]]).
24
List Concatenation n Want to put two lists together n Want to split a list in two ?- append([1, 2, 3], [4, 5, 6], L). L = [1, 2, 3, 4, 5, 6] Yes ?- append(L1, L2, [1, 2, 3, 4, 5, 6]). L1 = [], L2 = [1, 2, 3, 4, 5, 6] ; L1 = [1], L2 = [2, 3, 4, 5, 6] ;
25
The append/3 Relationship n append/3 can be used with any combination of lists and variables n It’s a relationship between the three lists n Picture append(L1, L2, L3): L1L2 L3 Doesn’t matter which one(s) you know ahead of time
26
Pictures of Lists n Making pictures of lists can help write code n Does list L have three zs together in it? ?- append(_, [z, z, z | _], L). 1 st sublist L _z, z, z_ 2 nd sublist
27
_ 1 st sublist L _ 2 nd sublist Pictures of Lists n Two sub-lists together make a longer list n Elements come at the front of some list ?- append(_, [z, z, z | _], L). z, z, z
28
Exercise n Draw a picture & write a question to ask whether a list L has two elements the same together –[1, 2, 2, 3, 4] does –[1, 2, 3, 2, 3] does not
29
Solution n Picture and code ?- append(_, [X, X | _], L). 1 st sublist L _X, X_ 2 nd sublist
30
Question to Predicate n Rewrite the query above as a predicate doubled_element/2 –first argument is the list –second argument is the doubled element doubled_element(L, X) :- append(_, [X, X | _], L). n Works with values or variables everywhere
31
Exercise n Draw a picture of a list that starts and ends with the same element n Write a question that asks whether L has this property (use append/3) n Translate the query into a predicate start_end/2 –1 st arg. is list, 2 nd is start/end element
32
Other Predicates with Lists n Facts about lists months([jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec]). –this is a list of the months (in order!) n Rules with lists is_month(M) :- months(MonthList), member(M, MonthList).
33
Note on Efficiency n months/1 is a fact with one clause –only one solution n member/2 –two clauses – one fact, one rule (use listing/1) –multiple solutions n Ask months/1 first –apply facts early –apply questions with less answers early
34
Note on Style n Having a months/1 predicate is (slightly!) slower than hand coding list into is_month n But it’s more general –can replace with full names… –…or with French, Spanish, … –use for other predicates that need months n Equivalent to declaring a constant –in style if not efficiency
35
Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X) for each element X of L n Identify the predicate & its argument(s) –we’ll talk about the ? soon n State the relationship/property that holds
36
Logic of List Testing n Every element is male n Going to use recursion (no loops!) n Base case –??? n Recursive case –??? What’s the simplest list of all males? No, there’s one simpler than that! What to do if it’s NOT the simplest case?
37
All Males % all_male(?L) %-- male(X) for all elements X of L all_male([]). all_male([H | T]) :- male(H),all_male(T).
38
General List Use n Common to want to do stuff with every element of a list –test it, make a copy of it, … n Multi-clause predicate (usually 2) –first clause is base case (usually 0 or 1 element) –last clause recursive case (extracts head, recurs on tail) –extra cases as required
39
On ?, + and – n Documented argument with ? in front of it –means that arg. can be variable or value n Alternatives are + –should have a value –unbound variables not allowed/bad idea n And – –should not have a value –unbound variables only “bound” = has a value other than just another variable Used in system documentation see help(member), help(is)
40
Exercise n Write a predicate that takes a fully filled in list of numbers and creates (or tests) a second list where each element is 1 more than in the first list –one_more([1,2,3], L) makes L=[2,3,4] –one_more([1,2,4], [2,3,5]) succeeds –one_more([1,2,5], [2,3,7]) fails n Document it
41
Case Analysis n Simplest list is the empty list n If the first list is empty, what should the second list be? n If the first list is not empty, how is the head of the second list related to the head of the first? n How are the tails related?
42
Documentation n How is the second list related to the first? n What notations on the arguments? –?? –+? ––? n What shall we call the arguments?
43
Possible Solution % one_more(+Less, ?More) %-- N is M+1 for each N in More % and the corresponding M in Less one_more([], []). one_more([M | Ms], [N | Ns]) :- N is M + 1, one_more(Ms, Ns).
44
Logical Addition %one_more_1(?Less, ?More) % -- N is M+1 for each N in More % and the corresponding M in Less % -- Note: N or M must be instantiated one_more_1([], []). one_more_1([M | Ms], [N | Ns]) :- plus(M, 1, N), one_more(Ms, Ns). % M + 1 is equal to N
45
Miscellaneous Math Stuff n Logical addition (will fill in any one variable) –plus(X, Y, Z) X + Y is equal to Z –succ(M, N) M + 1 is equal to N n Comparisons (variables must be bound) –M < N, N < P M < N & N < P –M = = P M N & N P –N \= PN P
46
By Cases in Prolog n Suppose we want to write larger/3 –3 rd argument is larger of 1 st two n Two cases: –first argument is larger than second –first argument is less than or equal to second % larger(+First, +Second, ?Larger) larger(First, Second, First) :- First > Second. larger(First, Second, Second) :- First =< Second.
47
Exercise n Write a predicate all_leq/2 –each element of the first list is less than or equal to the corresponding element of the second –(also, lists are the same length) ?- all_leq([1, 2, 3, 4], [1, 2, 3, 5]). Yes ?- all_leq([1, 2, 3, 4, 5], [7, 6, 5, 4, 3]). No
48
Getting Elements by Position n last/2 queries last element of a list n nth1/3 queries nth element of a list –starts counting at 1 n nth0/3 queries nth element of a list –starts counting at 0 ?- last(_Last, [a, c, e]), nth0(1, [a, c, e], _Second), nth1(2, L, _Second), nth1(1, L, _Last). L = [e, c | _G1]
49
Insertion and Deletion n Insertion to & deletion from lists –just opposites –three way relationship –shorter list, extra element, longer list –use same predicate for both –select/3 in SWI-Prolog
50
Using select/3 n To delete an element ?- select([g, o, n, e], n, L). L = [g, o, e] n To insert an element ?- select(L, n, [g, o, e]). L = [n, g, o, e] ; L = [g, n, o, e] ; L = [g, o, n, e] ;
51
Using select/3 n Deleting any element ?- select([a, b, c], X, Rest). X = a, Rest = [b, c] ; X = b, Rest = [a, c] ; X = c, Rest = [a, b] ; No
52
Using select/3 n To insert into a given position (1-based) ?- nth1(3, L, n), select(L, n, [g, o, e]). L = [g, o, n, e] Yes n Exercise: change that into a predicate % insertKth(Old, Kth, Value, New) %New is Old with Value inserted into Kth place %(first element of list = position 1)
53
Next Time n Using Structures and Lists
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.