Advertisement
Guest User

Untitled

a guest
Oct 11th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 0.82 KB | None | 0 0
  1. sum([ ], 0).
  2. sum([H|T], R) :- number(H), sum(T, Q), R is H+Q.
  3. sum([H|T], R) :- not(number(H)), sum(T, R).
  4.  
  5. % That's the cut operator, which basically states "if we got up to here, don't backtrack."
  6. % So, if both recursive calls succeed, the hi predicate will not retry them
  7.  
  8. % ! "cut" the search
  9.  
  10. sum2([ ], 0).
  11. sum2([H|T], R) :- number(H), !, sum2(T, Q), R is H+Q.
  12. sum2([H|T], R) :- sum2(T, R).
  13.  
  14. app([ ], L, L).
  15. app([H|T], L, [H|Z]) :- app(T, L, Z).
  16.  
  17.  
  18. rev([ ], [ ]).
  19. rev([H|T], L) :- rev(T, Q), app(Q, [H], L).
  20.  
  21.  
  22. flat([ ], [ ]).
  23. flat(X, [X]) :- atomic(X), not(X=[ ]).
  24. flat([H|T], R) :- flat(H, X), flat(T, Y), app(X, Y, R).
  25.  
  26.  
  27. % call - param 1 function, param 2 var to exeucte function with, param 3 var to give result to
  28.  
  29. map(F, [ ], [ ]).
  30. map(F, [H|T], [X|Y]) :- call(F, H, X), map(F, T, Y).
  31.  
  32. not(X) :- \+ X.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement