Lists_Part_2 - Note in a recursive definition, the problem...

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

View Full Document Right Arrow Icon
Examples Write the Prolog definition for being a list. islist([]). islist([Head|Tail]) :- islist(Tail). Write the Prolog definition for being a member of a list. member(X, [X|L]). member(X, [Y|L]) :- member(X,L). York University- CSE 3401 6 05_Lists
Background image of page 1

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

View Full DocumentRight Arrow Icon
Examples (cont.) :- member(3, [2, 3, 4, 5]). true :- member(3, [2, [3, 4], 5]). false :- member(X, [1, 2]). X = 1 ; X = 2 ; false :- member(2, L). L = [2|_] ; L=[_, 2 | _]; ... York University- CSE 3401 7 Our definition does not consider members of members (nested lists) Unlike other programming languages, inputs can be unknowns Note the recursive definition of member 05_Lists
Background image of page 2
Recursive Definition Example: member(X, [X|L]). : boundary condition member(X, [Y|L]) :- member(X,L). : recursive case
Background image of page 3

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

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Note in a recursive definition, the problem must get smaller in each recursion to guarantee convergence member(X,L) is a smaller problem than member(X, [Y|L]) Prolog does not need to remember Y, so we can use the anonymous variable _ instead: member(X, [_|L]) :- member(X,L). York University- CSE 3401 8 05_Lists Recursive Search Example: member(X, [X|L]). member(X, [Y|L]) :- member(X,L). :-member(X, [a,b,c]). X = a; X = b; X = c; false York University- CSE 3401 9 05_Lists Delete delete(X, L1, L2) is true if L2 is the result of deleting X from L1 (just once). For example: delete(5, [1, 5, 4, 2], [1, 4, 2]). delete(X, [X|L], L). delete(X, [Y|L], [Y|L1]) :- delete(X, L, L1). York University- CSE 3401 10 05_Lists...
View Full Document

Page1 / 5

Lists_Part_2 - Note in a recursive definition, the problem...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online