Lists_Part_3

Lists_Part_3 - prefix(P L append(P L – Is a prefix of L...

This preview shows pages 1–5. Sign up to view the full content.

Append Join two lists: Example: append([1,2], [3,4], [1,2,3,4]) append([], L, L). : boundary condition append([X|L1], L2, [X|L3]) :- append(L1, L2, L3). : recursive case a smaller problem Possible Queries: [Nilsson] :- append([a, b], [c, d], [a, b, c, d]). true :- append([a, b], [c, d], X). X=[a, b, c, d] or even :- append(Y, Z, [a, b, c, d]). York University- CSE 3401 11 05_Lists

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Search tree for append query append([], X, X). append([X|Y], Z, [X|W]) :- append(Y, Z, W). :- append(Y, Z, [a, b, c, d]). York University- CSE 3401 12 05_Lists
Prefix with append The list P is a prefix of L, if L can be obtained by appending P to another list. Write prefix(P,L) which is true if P is a prefix of L.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: prefix(P, L):- append(P, _, L). – Is a prefix of L? York University- CSE 3401 13 05_Lists Suffix with append • The List S is a suffix (or postfix) of L if L can be obtained by appending some other list with S. • Write suffix(S,L) which is true if S is a suffix of L suffix(S,L):- append(_, S, L). • Exercise: Try writing prefix and suffix without using append. York University- CSE 3401 14 05_Lists More Examples with append • sublist(S,L) is true if S is a sublist of L – in other words, S is the suffix of a prefix – Using append(. .): sublist(S,L):- append(_, S, Left), append(Left, _, L). York University- CSE 3401 15 05_Lists...
View Full Document

{[ snackBarMessage ]}

Page1 / 5

Lists_Part_3 - prefix(P L append(P L – Is a prefix of L...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online