Advertisement
Guest User

Untitled

a guest
Oct 7th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 2.52 KB | None | 0 0
  1. % =========================================================================
  2. % FILE     : itrlist.pl
  3. % SUPPORT  : Bapst Frederic, HEIA-FR.
  4. % CONTEXT  : Techniques Avancées de Programmation 3 : Programmation Logique
  5. % =========================================================================
  6. % OVERVIEW : Abstract Data Types in Prolog - Iterative lists
  7. % =========================================================================
  8.  
  9. % ------------------------------------------------------------
  10. % --- Test programs
  11. % ------------------------------------------------------------
  12. adt_test_apply(_, Adt, [], Adt).
  13. adt_test_apply(G, Adt, [C|Cmds], AdtEnd) :-
  14.         Goal =.. [G, Adt, C, Adt1],
  15.         write(trying(Goal)), nl,
  16.         Goal,
  17.         write(done(Goal)), nl,
  18.         adt_test_apply(G, Adt1, Cmds, AdtEnd).
  19. % ------------------------------------------------------------
  20. itrList_test :-
  21.         itrList_new(ItrList),
  22.         Cmds = [insertAfter(a),
  23.                 insertAfter(b),
  24.                 goToNext,
  25.                 consultAfter(a),
  26.                 removeAfter(a),
  27.                 isLast,
  28.                 insertAfter(d),
  29.                 goToPrev,
  30.                 consultAfter(B)
  31.                ],
  32.         adt_test_apply(itrList_apply, ItrList, Cmds, ItrList1),
  33.      \+ itrList_apply(ItrList1, isLast,_),
  34.         assertEquals(b, B),
  35.         write('Test passed successfully.').
  36.  
  37. assertEquals(Expected, Effective) :-
  38.     Expected==Effective.
  39. assertEquals(Expected, Effective) :-
  40.     Expected\==Effective,
  41.     write('bad news... Expected: '), write(Expected),
  42.     write(' Effective: '), write(Effective), nl,
  43.     fail.
  44. % ------------------------------------------------------------
  45. % itrList : Iterative List ADT, with current arc
  46. %    Operations :
  47. %      new,      goToNext,  insertAfter(+X),
  48. %      isFirst,  goToPrev, consultAfter(?X),
  49. %      isLast,              removeAfter(?Removed)
  50. %    Representation :
  51. %      e(ListTowardsFirst, ListTowardsEnd)
  52.  
  53. % itrList_new(...).
  54.  
  55. % itrList_apply(..., goToNext, ...).
  56. % itrList_apply(..., goToPrev, ...).
  57. % ...
  58.  
  59. itrList_new(e([],[])).
  60.  
  61. itrList_apply(e(X,[]),goToNext,e(X,[])).
  62. itrList_apply(e(Xs,[Y|Ys]),goToNext,e([Y|Xs],Ys)).
  63.  
  64. itrList_apply(e(X,Y),insertAfter(Val),e(X,[Val|Y])).
  65.  
  66. itrList_apply(e([],Y),isFirst,e([],Y)).
  67.  
  68. itrList_apply(e(X,[]),isLast,e(X,[])).
  69.  
  70. itrList_apply(e([],X),goToPrev,e([],X)).
  71. itrList_apply(e([X|Xs],Ys),goToPrev,e(Xs,[X|Ys])).
  72.  
  73. itrList_apply(e(X,[Y|Ys]),consultAfter(Y),e(X,[Y|Ys])).
  74.  
  75. itrList_apply(e(X,[Y|Ys]),removeAfter(Y),e(X,Ys)).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement