Advertisement
steffffffan

P2_14

Nov 20th, 2022
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 0.65 KB | None | 0 0
  1. %14.
  2. % a) Definiti un predicat care determina predecesorul unui numar
  3. % reprezentat cifra cu cifra intr-o lista. De ex: [1 9 3 6 0 0] =>
  4. % [1 9 3 5 9 9]
  5.  
  6. predh([],[],0):-!.
  7. predh([H],[H1],0):- H>0,!, H1 is H-1.
  8. predh([0],[9],1):-!.
  9. predh([H|T],R,0):-
  10.     predh(T,R1,C1),
  11.     H1 is H-C1,
  12.     H1>=0,!,
  13.     R=[H1|R1].
  14. predh([H|T],R,1):-
  15.     predh(T,R1,C1),
  16.     H1 is H-C1,
  17.     H1<0,!,
  18.     R=[9|R1].
  19.  
  20. % predh([1,0,0],R,C) ==> R=[0,9,9], C=0
  21. % va trebui sa eliminam 0-urile de la inceput
  22.  
  23. elim0([0],[0]):-!.
  24. elim0([0,X|T],[X|T]):-X\=0,!.
  25. elim0([0,0|T],R):-elim0([0|T],R).
  26.  
  27. pred(X,R):-predh(X,R1,_),elim0(R1,R).
  28. % pred([1,0,0],R) ==> R=[9,9] ^_^
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement