Advertisement
Tavi33

L4 v2 BIA

Mar 24th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. % This buffer is for notes you don't want to save.
  2. % If you want to create a file, visit that file with C-x C-f,
  3. % then enter the text in that file's own buffer.
  4.  
  5. %tree(5, tree(3, tree(1, null, null), tree(4, null, null)), tree(7,
  6. % null, null)).
  7.  
  8. %1.
  9. add(null, S) :- S is 0.
  10. add(tree(X,Y,Z), S) :- add(Y, S1), add(Z, S2), S is X + S1 + S2.
  11.  
  12. %2.
  13. count_leaves(null, Count) :- Count is 0.
  14. count_leaves(tree(_, null, null), Count) :- !, Count is 1.
  15. count_leaves(tree(_,Y,Z), Count) :- count_leaves(Y, Count1), count_leaves(Z, Count2), Count is Count1 + Count2.
  16.  
  17. %3.
  18. rootValue(tree(X, _, _), Y) :- Y is X.
  19. compute(tree(X, null, null), Result) :- number(X), !, Result is X.
  20. compute(tree(+, X, Y), Result) :- compute(X, Op1), compute(Y, Op2), Result is Op1 + Op2.
  21. compute(tree(-, X, Y), Result) :- compute(X, Op1), compute(Y, Op2), Result is Op1 - Op2.
  22. compute(tree(*, X, Y), Result) :- compute(X, Op1), compute(Y, Op2), Result is Op1 * Op2.
  23. compute(tree(/, X, Y), Result) :- compute(X, Op1), compute(Y, Op2), Result is Op1 / Op2.
  24.  
  25. %4.
  26. %
  27. convert(X, Result) :- Result = tree(X, null, null),number(X).
  28. convert(X+Y, Result) :- Result = tree(+, X1, Y1),convert(X, X1), convert(Y, Y1) .
  29. convert(X-Y, Result) :-Result = tree(-, X1, Y1), convert(X, X1), convert(Y, Y1).
  30. convert(X*Y, Result) :- Result = tree(*, X1, Y1),convert(X, X1), convert(Y, Y1).
  31. convert(X/Y, Result) :- Result = tree(/, X1, Y1),convert(X, X1), convert(Y, Y1) .
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement