Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 2.66 KB | None | 0 0
  1. %3.2.1 Write the predicate
  2. % append3(L1, L2, L3, R), which achieves the concatenation of 3 lists.
  3.  
  4. append3(L1, L2, L3, R):- append(L2, L3,R1), append(L1, R1,R).
  5.  
  6. %3.2.2  Write a predicate which adds an element at the beginning of a list.
  7. %
  8. addEl(X, L, [X|L]). % X e head-ul rezultatului, lista initiala e tail-ul rezultatlui
  9.  
  10. %3.2.3  Write a predicate which computes the sum of the elements of a list of integers.
  11. %
  12. sumEL([H|T], R) :- sumEl(T,R1), %1. mergi pana la finalul listei
  13.                 R is R1+H. % 3. R+= cate un element de la final spre inceput
  14. sumEl([],0).  %2. se initializeaza R cu 0
  15.  
  16. %3.3.1 Write a predicate which takes as input a list of integers, L,
  17. %and produces two lists: the list containing the even elements from L
  18. %and the list of odd elements from L.
  19.  
  20. separate([H|T], [H|Even] ,Odd):- 0 is H mod 2, % primu element e par, o sa il punem in Even cand se termina apelul
  21.                                 separate(T,Even,Odd). % mergem mai departe pe restul litei
  22. separate([H|T],Even,[H|Odd]):- 1 is H mod 2, % e impar
  23.                                 separate(T,Even,Odd).
  24. separate([],[],[]). % am ajuns la finalul listei, initializam rezultatele cu lista vida, de acum incep apelurile sa
  25.                     % se termine si se pun elementele in liste ( de la sfarsit spre inceput)
  26.  
  27. %3.3.2 Write a predicate which removes all the duplicate elements in a list
  28. %(keep either the first or the last occurrence).
  29. %
  30.  
  31. remove_dup([H|T], R):-member(H,T),  %daca primul element se gaseste in restul listei, nu il punem in rezultat
  32.                     remove_dup(T,R),!. %trecem la urmatoarele elemente
  33. remove_dup([H|T],[H|R]):-remove_dup(T,R). %daca nu, trecem la urmatoarele elemente,
  34.                                             %dar punem elementul in rezultat
  35. remove_dup([],[]). %cand am ajuns la sfarsit, initializam rezultatul cu []
  36.  
  37. %3.3.3. Write a predicate which replaces
  38. %all the occurrences of element K with NewK in list L.
  39. %
  40.  
  41. replace([H|T], K, NewK, [NewK|R]):- H=K, %primu el. din lista e acelasi cu K, punem NewK in rezultat
  42.                             replace(T,K,NewK,R). %trecem mai departe
  43. replace([H|T],K,NewK,[H|R]):- not(H=K), % nu trebuie neaparat scris ca la if else
  44.                                         % punem elementul il rezultat
  45.                                 replace(T,K,NewK,R).
  46. replace([],_,_,[]). % _ inseamna ca nu conteaza ce e acolo ca nu folosesti nicaieri
  47.  
  48. %3.3.4. Write a predicate which deletes every Kth element from a list.
  49. %
  50. drop_k([H|T], K, [H|R], PosCur):- not(0 is PosCur mod K),!, %nu trebuie sters, il punem in rezultat
  51.                                 PosCur1 is PosCur+1, %calculam pozitia urmatoare
  52.                                 drop_k(T,K,R,PosCur1).
  53. drop_k([_|T], K, R, PosCur):- PosCur1 is PosCur +1, %nu il punem pe H in rezultat
  54.                             drop_k(T,K,R,PosCur1).
  55. drop_k([],_,[],_).
  56.  
  57. % drop_k([1,2,3,4,5],2,R,1).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement