Download
slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
asserta(X). assertz(X). assert(X). r etract (X). r etractall (X). PowerPoint Presentation
Download Presentation
asserta(X). assertz(X). assert(X). r etract (X). r etractall (X).

asserta(X). assertz(X). assert(X). r etract (X). r etractall (X).

135 Vues Download Presentation
Télécharger la présentation

asserta(X). assertz(X). assert(X). r etract (X). r etractall (X).

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

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

  2. :-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 Lists

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

  4. length([ ], 0). length([H|T], Len) :- length(T, Len1), Len is Len1 + 1. ^ 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 [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 Lecture 16 More on Lists

  5. Removing an element from a list 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] Lecture 16 More on Lists

  6. Insertion Sort 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=<H. ?- isort([4,1,9,5,8,3,2],S). S = [1, 2, 3, 4, 5, 8, 9] ?- isort([a,b,c,d],S). ERROR: Arithmetic: `c/0' is not a function Lecture 16 More on Lists