AccDiff_p2 3

AccDiff_p2 3 - Difference List Why is it called a...

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

Difference List Why is it called a difference list? The name comes from list differences: [a,b,c,d,e] [d,e] = [a,b,c] [a,b,c|X]- X = [a,b,c] [L|Hole]- Hole= L for any list L and any list assigned to Hole The idea is to have a HOLE in the tail of a list to be instantiated later by Prolog. A list L is represented by the difference between another list in the form [L|Hole] and one of its sublists (the tail of the list, Hole) that must be an unknown. The empty list is represented by X-X [a,b,c] is represented by [a,b,c|X]-X York University- CSE 3401- V. Movahedi 22 06_AccDiff

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

View Full Document
Reverse using difference lists reverse(L,R) :- rev(L, R-[]). rev([], W-W). rev([X|T], Z-W) :- rev(T, Z- [X|W]). :- reverse([a,b], R). :- rev([a,b], R-[]). :- rev([b], R-[a]). :- rev([], R- [b,a]). R=[b,a] Can reverse a list of n elements in n+2 resolution steps. York University- CSE 3401- V. Movahedi 23 06_AccDiff
Reverse using difference lists (cont.) reverse(L,R) :- rev(L, R-[]). rev([], W-W).

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.

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

AccDiff_p2 3 - Difference List Why is it called a...

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

View Full Document
Ask a homework question - tutors are online