Advertisement
denyshubh

Untitled

Mar 28th, 2023 (edited)
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 0.79 KB | None | 0 0
  1. % Base case: if PlusExpr is atomic, then LeftPlusExpr is also PlusExpr
  2. left_plus(PlusExpr, LeftPlusExpr) :-
  3.     atomic(PlusExpr),
  4.     LeftPlusExpr = PlusExpr.
  5.  
  6. % Recursive case: PlusExpr is in the form A+B, where A and B are unrestricted plus expressions
  7. left_plus(A + B, LeftPlusExpr) :-
  8.     % Recurse on A
  9.     left_plus(A, LeftA),
  10.    
  11.     % If B is atomic, then LeftPlusExpr is LeftA + B
  12.     (   atomic(B)
  13.     ->  LeftPlusExpr = LeftA + B
  14.     ;   % Else, B is in the form B1 + B2
  15.         B = B1 + B2,
  16.        
  17.         % Recurse on B1 + B2
  18.         left_plus(B1 + B2, LeftB),
  19.        
  20.         % Extract the left-most atomic from LeftB
  21.         LeftB = LeftB1 + LeftB2,
  22.        
  23.         % LeftPlusExpr is LeftA + LeftB1 + LeftB2
  24.         LeftPlusExpr = LeftA + LeftB1 + LeftB2
  25.     ).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement