Advertisement
Haath

Untitled

May 26th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 2.31 KB | None | 0 0
  1. geovlahodim@gmail.com
  2.  
  3. fact(0, 1).
  4. fact(N, newF) :- N>0, NewN is N - 1, fact(NewN, F), NewF is N * F.
  5.                                        
  6.  
  7. sum([], 0).
  8. sum([H | T], N) :- sum(T, NT), N is H + NT.
  9.       #^ list head+tail
  10.        
  11.     #with accumulator
  12. sum2([], Acc, Acc). #<-- result
  13. sum2([H | T], Acc, Res) :- NewAcc is H + Acc, sum2(T, NewAcc, Res).
  14.  
  15.     #simplifying the call
  16. sum3(L, N) :- sum2(L, 0, N).
  17.  
  18. rev([],[]).
  19. rev([H | T], R) :- rev(T, RT), append(RT, [H], R).
  20.                         # append(L1, L2, LF) =>  LF = L1@L2
  21.  
  22. anagrams(S1, S2, Moves) :-
  23.         string_to_list(S1, L1),
  24.         reverse(L1, RL1),  #reverse is built-in
  25.         string_to_list(S2, L2),
  26.         reverse(L2, RL2),
  27.         length(Moves, _), #Initialy produces []
  28.         solve(RL1, [], [], Moves, RL2), !.
  29.         #after it fails it backtracks and gets Moves=[H]
  30.         #'!' means no backtracking beyond success point
  31.        
  32.     # works for checking. both lists must exist
  33. is_permutation_of(L1, L2) :-
  34.         msort(L1, S),   #sorting two lists with same items
  35.         msort(L2, S).   #should give the same list
  36.        
  37.        
  38.     # better way, works for producing aswell
  39. is_permutation_of2([], []).
  40. is_permutation_of2(L1, [X|LL2]) :-
  41.         select(X, L1, LL1), # gets an item from L1, returns it on X. LL1 is the remaining
  42.         is_permutation_of(LL1, LL2).
  43.        
  44.        
  45. length([], 0).
  46. length([_ | T], N) :- length(T, N1, N is N1 + 1.
  47.        
  48.              #1   C    2
  49. solve([], [], Goal, [], Goal).
  50. solve([H|T], [], L2, ['10'|Moves], Goal) :-
  51.         allowed_from('10', Moves),
  52.         solve(T, [H], L2, Moves, Goal).
  53. solve([H|T], C, L2, ['12'|Moves], Goal) :-
  54.         solve(T, C, [H | L2], Moves, Goal).
  55. solve(L1, [], [H|T], ['20'|Moves], Goal) :-  
  56.         solve(L1, [H], T, Moves, Goal).
  57. solve(L1, C, [H|T], ['21'|Moves], Goal) :-  
  58.         solve([H|L1], C, T, Moves, Goal).
  59. solve(L1, [H], L2, ['01'|Moves], Goal) :-  
  60.         solve([H|L1], [], L2, Moves, Goal).
  61. solve(L1, [H], L2, ['02'|Moves], Goal) :-  
  62.         solve(L1, [], [H|L2], Moves, Goal).
  63.  
  64. #Filters allowed move based on previous move
  65. #we list moves we dont want in sequence
  66. allowed_from(_, []).
  67. allowed_from(X, [Y | _]) :- allowed_next(X,Y).
  68.  
  69. #listing all allowed consequent moves
  70. allowed_next('10', Y) :- \+ member(Y, ['10', '01', '02']
  71. allowed_next('12', Y) :- \+ member(Y, ['12', '01', '02']
  72. allowed_next('20', Y) :- \+ member(Y, ['12', '21', '02']
  73. allowed_next('10', Y) :- \+ member(Y, ['10', '01', '02'] #INCOMPLETE
  74. # '\+' means 'not'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement