Advertisement
denyshubh

Untitled

Mar 28th, 2023 (edited)
695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 0.52 KB | None | 0 0
  1. left_plus(Expr, Left) :- left_plus(Expr, 0, Left).
  2.  
  3. % Base case: return the left plus expression when the expression is an atomic
  4. left_plus(Atomic, Acc, Acc + Atomic) :- atomic(Atomic).
  5.  
  6. % Recursive case: recurse left-to-right, accumulating atomics into a left plus expression accumulator
  7. left_plus(Left + Right, Acc, Result) :-
  8.     atomic(Left),
  9.     left_plus(Right, Acc + Left, Result).
  10.  
  11. left_plus(Left + Right, Acc, Result) :-
  12.     \+ atomic(Left),
  13.     left_plus(Left, Acc, Acc2),
  14.     left_plus(Right, Acc2, Result).
  15.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement