ElenaR1

Prolog 1st exercise

Oct 15th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 2.57 KB | None | 0 0
  1. [_,_,[_|X]|Tai]=[[],0,[1,2,3],4,5,6]
  2. Tai = [4, 5, 6],
  3. X = [2, 3]
  4.  
  5. add(X,L,N).
  6. add(X,[],[X]).
  7. add(X,[Y],[Y,X|[]]).
  8. add(X,[H|T],[H|N]):-add(X,T,N).
  9.  
  10. memberr(X,[X|T]).
  11. memberr(X,[H|T]):-
  12.     memberr(X,T).
  13.  
  14. member(X,[23,50,16,14,30,14]), Y is X*X, Y<400.
  15. X = 16,
  16. Y = 256
  17. X = 14,
  18. Y = 196
  19. X = 14,
  20. Y = 196
  21.  
  22.  
  23.  
  24. first(X,[X|L]).
  25. first(X,[[5,2],3,4]).
  26. X = [5, 2]
  27.  
  28. snd(X,[_,X|L]).
  29. X = 3
  30.  
  31. lastt(X,[X]).//IF i ONLY WIRTE E.G A(X,[X]). and a(X,[2]). it will give me 2
  32. lastt(X,[_|L]):-
  33.     lastt(X,L).
  34. lastt(X,[[5,2],3,4]).
  35. X=4
  36.  
  37.  
  38. append([],B,B).
  39. append([H|A],B,[H|AB]):-
  40.     append(A,B,AB).
  41. append([1,2],[3,4],X).
  42. //opisano v tetradkata-stiga do dunoto,koeto e ([],B,B). 2roto B nqma stoinost tui kato AB nqma => 2roto B priema stoinostta na 1voto.
  43. //B=[3,4]->AB=[3,4] i se vrushtame nazad 2|AB->[2,3,4], 1|AB->[1,2,3,4]
  44. https://www.youtube.com/watch?v=0LR_N_EMjwk
  45.  
  46. 2nd exercise
  47. 1member using append
  48. append([],B,B).
  49. append([H|A],B,[H|AB]):-
  50.     append(A,B,AB).
  51.  
  52. member(X,L):-
  53.     append(A,[X],C),
  54.     append(C,B,L),
  55.     append(_,[X|_],L).
  56. member(2,[1,2,3,4])
  57. true
  58.  
  59.  
  60. 2last using append
  61. last(X,L):-append(_,[X],L).
  62.  
  63. PREFIX
  64. prefix(P,L):-append(P,_,L).
  65. This says that list P is a prefix of list L when there is some list such that L is the result of concatenating P with that list. (We use the anonymous variable since we donโ€™t care what that other list is: we only care that there is some such list or other.) This predicate successfully finds prefixes of lists, and moreover, via backtracking, finds them all:
  66. refix(X,[1,2,3,4]).
  67. X = []
  68. X = [1]
  69. X = [1, 2]
  70. X = [1, 2, 3]
  71. X = [1, 2, 3, 4]
  72.  
  73. suffix(S,L):-append(_,S,L).
  74. That is, list S is a suffix of list L if there is some list such that L is the result of concatenating that list with S . This predicate successfully finds suffixes of lists, and moreover, via backtracking, finds them all:
  75. suffix(X,[1,2,3])
  76. X = [1, 2, 3]
  77. X = [2, 3]
  78. X = [3]
  79. X = []
  80.  
  81.  
  82.  
  83. ??
  84. infix(A,L):-append(P,_,L),
  85.     append(_,A,P).
  86.  
  87. //infix(A,[1,2,3])
  88. A = []
  89. A = [1]
  90. A = []
  91. A = [1, 2]
  92. A = [2]
  93. A = []
  94. A = [1, 2, 3]
  95. A = [2, 3]
  96. A = [3]
  97. A = []
  98.  
  99.  
  100. %dali S e pod-vo na L
  101.  
  102. subset([],[]).
  103.         subset([X|S],[X|L]) :-
  104.             subset(S,L).
  105.         subset(S, [_|L]) :-
  106.             subset(S,L).
  107. subset(P,[1,2,3])
  108. P = [1, 2, 3]
  109. P = [1, 2]
  110. P = [1, 3]
  111. P = [1]
  112. P = [2, 3]
  113. P = [2]
  114. P = [3]
  115. P = []
  116.  
  117.  
  118. del(X,[X|Tail],Tail).
  119.  
  120. del(X,[Head|Tail],[Head|NewList]):-
  121.     del(X,Tail,NewList).
  122.  
  123. remove(X,L,N):-append(A,[X|B],L),
  124.     append(A,B,N).
  125. remove(X,[X|L],L).
  126. remove(X,[H|L],[H|B]):-remove(X,L,B).
  127.  
  128. add(X,L,N):-remove(X,N,L).
Add Comment
Please, Sign In to add comment