# Lecture 16 More on Lists1 asserta(X). assertz(X). assert(X). retract(X). retractall(X).

## Presentation on theme: "Lecture 16 More on Lists1 asserta(X). assertz(X). assert(X). retract(X). retractall(X)."— Presentation transcript:

Lecture 16 More on Lists1 asserta(X). assertz(X). assert(X). retract(X). retractall(X).

Lecture 16 More on Lists2 :-dynamic animal/1. % A directive. animal(tiger). animal(lion). animal(monkey). animal(X):-mamal(X), asserta(animal(X)). mamal(cat). mamal(dog). ?- animal(dog). Yes ?- listing(animal). :- dynamic animal/1. animal(dog). animal(tiger). animal(lion). animal(monkey). animal(A) :- mamal(A), asserta(animal(A)).

Lecture 16 More on Lists3 length([ ], 0). length([H|T], Len) :- length(T, Len1), Len is Len1 + 1. Finding the length of a list

Lecture 16 More on Lists4 [trace] ?- leng([1,2,3,4,5],Ln). Call: (6) leng([1, 2, 3, 4, 5], _G405)?creep Call: (7) leng([2, 3, 4, 5], _G474) ? creep Call: (8) leng([3, 4, 5], _G474) ? creep Call: (9) leng([4, 5], _G474) ? creep Call: (10) leng([5], _G474) ? creep Call: (11) leng([], _G474) ? creep Exit: (11) leng([], 0) ? creep ^ Call: (11) _G479 is 0+1 ? creep ^ Exit: (11) 1 is 0+1 ? creep Exit: (10) leng([5], 1) ? creep ^ Call: (10) _G482 is 1+1 ? creep ^ Exit: (10) 2 is 1+1 ? creep Exit: (9) leng([4, 5], 2) ? creep ^ Call: (9) _G485 is 2+1 ? creep ^ Exit: (9) 3 is 2+1 ? creep Exit: (8) leng([3, 4, 5], 3) ? creep ^ Call: (8) _G488 is 3+1 ? creep ^ Exit: (8) 4 is 3+1 ? creep Exit: (7) leng([2, 3, 4, 5], 4) ? creep ^ Call: (7) _G405 is 4+1 ? creep ^ Exit: (7) 5 is 4+1 ? creep Exit: (6) leng([1, 2, 3, 4, 5], 5) ? creep Ln = 5 Yes length([ ], 0). length([H|T], Len) :- length(T, Len1), Len is Len1 + 1.

Lecture 16 More on Lists5 remove(X, [ ], [ ]). remove(X, [X|T], C):- remove(X,T,C). remove(X, [H|T], [H|T1]):- X \= H, remove(X,T,T1). ?- remove(1,[4,9,8,7,1,9,7,5,1],R). R = [4, 9, 8, 7, 9, 7, 5] Removing an element from a list

Lecture 16 More on Lists6 isort([], []). isort([H|T],R):- isort(T,Q), ins(H,Q,R). ins(X, [], [X]). ins(X, [H|T], [H|R]):- X>H, ins(X,T,R). ins(X, [H|T], [X,H|T]):- X= { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/3814910/slides/slide_6.jpg", "name": "Lecture 16 More on Lists6 isort([], []). isort([H|T],R):- isort(T,Q), ins(H,Q,R).", "description": "ins(X, [], [X]). ins(X, [H|T], [H|R]):- X>H, ins(X,T,R). ins(X, [H|T], [X,H|T]):- X=

Download ppt "Lecture 16 More on Lists1 asserta(X). assertz(X). assert(X). retract(X). retractall(X)."

Similar presentations