montimaj

PROLOG

Nov 29th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 4.21 KB | None | 0 0
  1. fact(0,1).
  2. fact(1,1).
  3. fact(N, F):- N>1, N1 is N-1, fact(N1, F1), F is N*F1. %Factorial of an integer
  4.  
  5. sumfact(U,U,S):- fact(U,F), S is F.
  6. sumfact(U,V,S):- V>=U, fact(V,F), X is V-1, sumfact(U,X,S1), S is S1+F. %Sum of factorials within a range
  7.  
  8. mypow(X,1,X).
  9. mypow(X,0,1).
  10. mypow(X,Y,P):- Y>0, Y1 is Y-1, mypow(X,Y1,P1), P is X*P1.
  11. mypow(X,Y,P):- Y<0, Y1 is -Y, mypow(X,Y1,P1), P is 1/P1. %Calculate X^Y
  12.  
  13. myexp(P,0,1).
  14. myexp(P,Q,E):- Q>0, Q1 is Q-1, myexp(P,Q1,E1), fact(Q, FACT), mypow(P,Q,POW), E is E1 + POW/FACT, !. % E = 1+P+P^2/2!+...+P^Q/Q!
  15.  
  16. gcd(A,B,G):- R is A mod B, R=0, G is B.
  17. gcd(A,B,G):- R is A mod B, R>0, gcd(B,R,G). % hcf of two nos.
  18.  
  19.  
  20. myappend([],L2,L2).
  21. myappend([H|L1],L2,[H|T]):-myappend(L1,L2,T). % append two lists
  22.  
  23. count([_|Y],C):- count(Y,C1), C is C1+1.
  24. count([],0). % length of a list
  25.  
  26. cpf([X|Y],N,[X|L]):- N1 is N-1, cpf(Y,N1,L).
  27. cpf([X|Y],1,[X|[]]). % copy first N elements of a list
  28.  
  29. tail([X|Y],N,L):- N1 is N-1, tail(Y,N1,L).
  30. tail([_|Y],1,Y). % show last N elements of a list
  31.  
  32. cpl([X|Y],N,L):- count([X|Y],C), N1 is C-N, tail([X|Y],N1,L).
  33. cpl([_|Y],1,Y). % copy last N elements of a list
  34.  
  35. dividelist(L,A,B):- count(L,C), C1 is round(C/2), C2 is C-C1, cpf(L,C1,A), cpl(L,C2,B). % divide a list equally
  36.  
  37. splice(L,1,N,T):-cpf(L,N,T).
  38. splice(L,M,N,T):- M1 is M-1,N1 is N-M1, tail(L,M1,T1), cpf(T1,N1,T). // extract M to N indices from a list
  39.  
  40. last(L,K):- count(L,C), C1 is C-1, tail(L,C1,K). % last element of a list
  41.  
  42. find([H|L],K,X):- K1 is K-1, find(L,K1,X).
  43. find([H|L],1,H).  % find element in Kth position
  44.  
  45. search([H|L],K,X):- K=H, X is H, write('Found'),!.
  46. search([H|L],K,X):- count([H|L],C), C1 is C-1, search(L,K,X).
  47. search([],_,_):- write('Not found'). %Linear search
  48.  
  49. reverse([A|[]],[A|[]]).
  50. reverse([H|L],T):- reverse(L,T1), myappend(T1,[H|[]],T). %Reverse list
  51.  
  52. replace(L,P,K,T):- P1 is P-1, cpf(L,P1,X),tail(L,P,Y), myappend(X,[K|[]],T1), myappend(T1,Y,T). %replace a given position with a new element
  53.  
  54. insert(L,P,K,T):- P1 is P-1, cpf(L,P1,X), tail(L,P1,Y), myappend(X,[K|[]],T1), myappend(T1,Y,T). %insert at a given position
  55.  
  56. delete(L,P,T):- P1 is P-1, cpf(L,P1,X), tail(L,P,Y), myappend(X,Y,T). %delete element from a given position
  57.  
  58.  
  59. min([A|[]],A). % Minimum element in a list
  60. less(A,B,R):- A=<B, R is A.
  61. less(A,B,R):- B<A, R is B.
  62. min([A|B],M):- min(B,M1),less(A,M1,M).
  63.  
  64. dellist([],_,[]).
  65. dellist([X|Y],X,Y).
  66. dellist([X|Y],M,N):-dellist(Y,M,N1), myappend([X|[]],N1,N). %Delete a specified element from a list
  67.  
  68.  
  69. selectionsort([A|[]],[A|[]]).
  70. selectionsort([A|B],[X|Y]):- min([A|B],X), dellist([A|B],X,Y1), selectionsort(Y1,Y). % Selection sort
  71.  
  72. palindrome(A):- reverse(A,B), A\=B, write('Not palindrome').
  73. palindrome(A):- reverse(A,B), A=B, write('Palindrome'). % Checks whether a list is palindrome
  74.  
  75. rduplist([],_,[]).
  76. rduplist([A|[]],A,[]).
  77. rduplist([A|[]],_,[A|[]]).
  78. rduplist([X|Y],X,Z):- rduplist(Y,X,Z).
  79. rduplist([X|Y],M,R):- rduplist(Y,M,R1), myappend([X|[]],R1,R),!. %Remove all duplicates of a specified no.
  80.  
  81. rduplistall([],[]).
  82. rduplistall([X|Y],[X|L]):- rduplist(Y,X,L1),rduplistall(L1,L). %Remove all duplicates from a list.
  83.  
  84.  
  85. rleft([],_,[]).
  86. rleft([X|Y],N,L):- cpf([X|Y],N,L1), tail([X|Y],N,L2), myappend(L2,L1,L). %rotate left
  87.  
  88. rright([],_,[]).
  89. rright([X|Y],N,L):- count([X|Y],C), N1 is C-N, cpf([X|Y],N1,L1), cpl([X|Y],N,L2),myappend(L2,L1,L). %rotate right
  90.  
  91.  
  92. tree(1, tree(2, tree(3,nil,nil), tree(4,tree(5,nil,nil), tree(6,nil,nil))), tree(7, nil, tree(8,tree(9,nil,nil),nil))).
  93. preorder(tree(X,_,_),X).
  94. preorder(tree(_,L,_),X):- inorder(L,X).
  95. preorder(tree(_,_,R),X):- inorder(R,X).
  96.  
  97. inorder(tree(_,L,_),X):- inorder(L,X).
  98. inorder(tree(X,_,_),X).
  99. inorder(tree(_,_,R),X):- inorder(R,X).
  100.  
  101. postorder(tree(_,L,_),X):- postorder(L,X).
  102. preorder(tree(_,_,R),X):- postorder(R,X).
  103. preorder(tree(X,_,_),X).
  104.  
  105. leaves(nil,0).
  106. leaves(tree(_,nil,nil),1).
  107. leaves(tree(_,L,R),C):-  leaves(L,C1), leaves(R,C2), C is C1+C2.
  108.  
  109. edge(1,2).
  110. edge(2,3).
  111. edge(2,1).
  112. edge(3,1).
  113. edge(3,1).
  114. edge(3,4).
  115.  
  116. vertex(1).
  117. vertex(2).
  118. vertex(3).
  119. vertex(4).
  120.  
  121. path(U,V,P):- vertex(U), vertex(V),edge(U,V), P is 1.
  122. path(U,V,P):- vertex(U), vertex(V),edge(U,X), path(X,V,P), P is 1,!.
  123.  
  124. cycle(U,P):- edge(U,U), P is 1.
  125. cycle(U,P):- path(U,X,_), path(X,U,_), !, P is 1.
Add Comment
Please, Sign In to add comment