Advertisement
steffffffan

P2_1

Nov 20th, 2022 (edited)
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 0.65 KB | None | 0 0
  1. addf([],E,[E]).
  2. addf([H|T],E,[H|R]):-addf(T,E,R).
  3.  
  4. invl([],[]):-!.
  5. invl([H|T],R):-invl(T,R1), addf(R1,H,R).
  6.  
  7. sumh([],[],[],0):-!.
  8. sumh([],[],[1],1):-!.
  9. sumh([],[H|T],R,C):-!,sumh([H|T],[],R,C).
  10. sumh(L,[],L,0):-!.
  11. sumh([H|T],[],R,1):-!,sumh([H|T],[1],R,0).
  12. sumh([HA|TA],[HB|TB],R,C):-
  13.     H is HA+HB+C,
  14.     C1 is H // 10,
  15.     H1 is H mod 10,
  16.     sumh(TA,TB,R1,C1),
  17.     R=[H1|R1].
  18.  
  19.  
  20. suma(A,B,R):-
  21.     invl(A,AI),
  22.     invl(B,BI),
  23.     sumh(AI,BI,RI,0),
  24.     invl(RI,R).
  25.  
  26. sumalist([],[0]).
  27. sumalist([H|T],R):-
  28.     is_list(H), !,
  29.     sumalist(T,R1),
  30.     suma(H,R1,R).
  31. sumalist([H|T],R):-
  32.     not(is_list(H)),
  33.     sumalist(T,R).
  34.  
  35.  
  36.  
  37.  
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement