Advertisement
esterfarbstein

Question 3

Dec 10th, 2018
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 0.77 KB | None | 0 0
  1. simplify1(end):-!.
  2. simplify1(Exp+X):-
  3.     (  (retract(v(X,N)),!,  %% I put the retract in the "or" and changed the sides of the "or"
  4.         N1 is N+1,
  5.         assert(v(X,N1)))
  6.     ;
  7.         assert(v(X,1))
  8.     ),
  9.     simplify1(Exp).
  10. simplify1(X):-
  11.     atomic(X),  %% I add the atomic check
  12.     simplify1(end+X).
  13.  
  14.  
  15. list_to_output([C/N|List],Sum,Out):-
  16.     list_to_output(List,Sum1,Out1), %% I changed this line place
  17.     (   atom(C),
  18.         N>1,!,
  19.         Out=Out1+C*N,
  20.         Sum = Sum1 %% I added it
  21.     ;   atom(C),!,
  22.         Out=Out1+C,
  23.         Sum = Sum1 %% I added it
  24.     ;   integer(C),
  25.         Sum is Sum1+(C*N),
  26.         Out = Out1 % I added it
  27.     ).
  28. list_to_output([],0,'').
  29.  
  30. simplify(Expression,Out1):-
  31.     simplify1(Expression),
  32.     findall(C/N,v(C,N),List),
  33.     list_to_output(List,Sum,Out),
  34.     Out1 =Out+Sum.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement