Advertisement
xFazz

Prolog notes

Dec 7th, 2023
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. swipl
  2. [test].
  3. man(john).
  4.  
  5. Find the movies released after 1990:
  6. movie(m, y), y > 1990.
  7.  
  8. Use semicolon (;, represents or) to cycle through results
  9.  
  10. Find the movies released before 2000, but after 1995
  11. movie(m, y), y < 2000, y > 1995
  12.  
  13. [moviesdb].
  14. [user].
  15. |: released_after(M, Y) :- movie(M, Z), Z > Y.
  16. (ctrl d to exit editor mode)
  17.  
  18. ?- 4 = 2 + 2.
  19. false.
  20. (Returns false because not = not an equality operator, it's a unification operator)
  21.  
  22. ?- 4=:= 2+2.
  23. true.
  24.  
  25. ?- A = 4, B = 2 + 2, A =:= B.
  26. (first returns true because variable matches/unifies any complex term, second returns true because variable unifies with complex terms)
  27.  
  28. ?- A=4, B is 2+2.
  29. A=B, B=4. (is is the assignment operator)
  30.  
  31. List is head and tail.
  32. [H | T] = [a, b, c, d]
  33. H = a
  34. T = b, c, d
  35.  
  36. [H | T] = [a].
  37. H = a,
  38. T = [].
  39.  
  40. [_,_,X|T] = [john, sam, 13, abc].
  41. X = 13,
  42. T = [abc].
  43.  
  44. square(IN, OUT) :- OUT is IN * IN.
  45.  
  46. write_list([]).
  47. write_list([H|T]) :- write(H), nl, write_list(T). (prints the head, prints newline, goes to the tail of the list)
  48.  
  49. len([],0).
  50. len([H|T],L) :- len(T, L1), L is L1 + 1.
  51.  
  52. RECURSION: needs a basecase
  53. len([], 0). (base case, list is empty)
  54. len([_|T], OUT) :- len(T, OUT1), OUT is OUT1 + 1.
  55.  
  56. ^ Keeps calling itself on tail of the list. Last call returns 0, 0 + 1 is then put in output variable.
  57. is = assignment operator
  58. Singleton variable warning means var name is not used. Replace H with _ to avoid this
  59.  
  60. Now we want to write the function reCons([a,b,c], OUT).
  61. OUTput should be = [a,b,c].
  62.  
  63. Base case? reCons([], []).
  64. reCons([H|T],[H|T1]) :- reCons(T,T1).
  65.  
  66. ^ When we get to the end of the list, T1 is going to be unified with empty list when it hits the base case
  67. Everytime it goes back it puts the current H on the list
  68. To do recursion, we keep going to the tail of the list, till we hit the base case
  69.  
  70. Exercise!
  71. Create a function square([1,2,3],LIST).
  72. output: LIST=[1,4,9].
  73.  
  74. square([],[]).
  75. square([H|T],[H1|T1]) :- square(T,T1), H1 is H * H.
  76.  
  77. ^ H1 is the head of the output list. We calculate a new list that is unified with H1
  78.  
  79. Output of exercise:
  80.  
  81. ?- [user].
  82. |: square([],[]).
  83. |: square([H|T],[H1|T1]) :- square(T,T1), H1 is H * H.
  84. |: ^D% user://1 compiled 0.01 sec, 2 clauses
  85. true.
  86.  
  87. ?- square([1,2,3], LIST).
  88. LIST = [1, 4, 9].
  89.  
  90. ?- trace.
  91. true.
  92.  
  93. [trace] ....
  94.  
  95. Exercise!
  96. Traverse two lists and make sure each element in the second list is the square of the element in the first list. E.g,
  97. ([1,2,3],[1,4,9])
  98. Use equality operator (=:=)
  99.  
  100. Base case: both lists should be empty. Recursive call, go to the end
  101. isSquare([],[]).
  102. isSquare([H|T],[H1|T1]) :- isSquare(T, T1), S is H * H, S =:= H1.
  103.  
  104. ^ Each predicate is separated by commans (and). Each predicate must be true
  105.  
  106. isSquare([H|T],[H1|T1]) :- S is H * H, S =:= H1; S1 is H1 * H1, S1 =:= H, isSquare(T,T1).
  107.  
  108. ^ Two conditons that are anded, OR, two conditions that are anded, then recursive call
  109. This works for isSquare([1,4,9],[1,2,3])
  110.  
  111. Tail recursion: operation happens first, then recursion happens. At the beginning we need to introduce an accumulator value
  112. that holds the calculation
  113.  
  114. len1([H|T],L) :- helper([H|T],L,0).
  115. helper([],ACC,ACC). (this is the base case)
  116. helper([_|T],L,ACC) :- ACC1 is ACC + 1, helper(T,L,ACC1).
  117.  
  118. ^Initial value of accumalator is zero. Base case is when list is empty. L is the output variable, which then unifies with ACC.
  119. Third line: calculation happens, then is passed to the next recursive call
  120. End of file error means missing period (.) terminator
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement