Lists_Part_2

# Lists_Part_2 - • Note in a recursive definition the...

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

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

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

View Full Document
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
Recursive Definition Example: member(X, [X|L]). : boundary condition member(X, [Y|L]) :- member(X,L). : recursive case

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: • 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

## This note was uploaded on 02/14/2012 for the course CSE 3401 taught by Professor Movahedi during the Fall '11 term at York University.

### Page1 / 5

Lists_Part_2 - • Note in a recursive definition the...

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

View Full Document
Ask a homework question - tutors are online