Facebook
From shamim Ahmed, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 166
  1. Example 1: Basic Syntax and Input/Output
  2. Example Program:
  3. % Hello World program in Prolog
  4. hello_world :-
  5. write('Hello, World!').
  6.  
  7. greet :-
  8. write('What is your name? '),
  9. read(Name),
  10. format('Hello, ~w!', [Name]).
  11. Practice Exercises:
  12. 1. Modify the hello_world program to greet the user with "Goodbye!" instead.
  13. 2. Enhance the greet program to ask for the user's age and respond accordingly.
  14. Example 2: Basic Operations
  15. Example Program:
  16. % Arithmetic operations
  17. add(X, Y, Result) :- Result is X + Y.
  18. subtract(X, Y, Result) :- Result is X - Y.
  19.  
  20. % Factorial calculation
  21. factorial(0, 1).
  22. factorial(N, Result) :-
  23. N > 0,
  24. N1 is N - 1,
  25. factorial(N1, SubResult),
  26. Result is N * SubResult.
  27.  
  28. Practice Exercises:
  29. 1. Implement a Prolog predicate to calculate the square of a number.
  30.  
  31. 2. Write a predicate to compute the nth Fibonacci number.
  32. Example 3: Conditional Statements
  33. Example Program:
  34. % If-Else Statements
  35. is_even(X) :-
  36. 0 is X mod 2,
  37. write('Even').
  38. is_even(X) :-
  39. 1 is X mod 2,
  40. write('Odd').
  41.  
  42. % Comparison Operators
  43. is_greater(X, Y) :-
  44. X > Y,
  45. write('X is greater than Y').
  46. is_greater(X, Y) :-
  47. X =< Y,
  48. write('X is not greater than Y').
  49.  
  50. Practice Exercises:
  51. 1. Create a Prolog predicate to determine if a number is positive, negative, or zero.
  52. 2. Write a program to check if a given year is a leap year or not.
  53. Example 4: Lists and Cut Operator
  54. Example Program:
  55. % List Operations
  56. list_length([], 0).
  57. list_length([_|T], Len) :-
  58. list_length(T, Len1),
  59. Len is Len1 + 1.
  60.  
  61. % Using Cut Operator
  62. is_positive(X) :-
  63. X > 0,
  64. !.
  65. is_positive(X) :-
  66. X =< 0.
  67. Practice Exercises:
  68. 1. Implement a predicate to find the maximum element in a list.
  69. 2. Write a Prolog rule to remove duplicates from a list.
  70. Example 5: Debugging
  71. Example Program (with errors):
  72. % Debugging Exercise
  73. greater_than_ten(X) :-
  74. X > 10,
  75. write('Greater than 10').
  76. greater_than_ten(X) :-
  77. X =< 10,
  78. write('Not greater than 10').
  79. Practice Exercises:
  80. 1. Identify and correct the errors in the greater_than_ten predicate.
  81. 2. Debug a faulty program that is intended to find the sum of elements in a list.
  82. These examples and practice exercises cover a range of topics in Prolog programming, providing hands-
  83. on experience with basic syntax, operations, conditional statements, lists, cut operator, and debugging
  84. techniques.
  85.  
  86. Family Tree problems:
  87. % Facts
  88. male(john).
  89.  
  90. male(peter).
  91. male(mike).
  92. female(mary).
  93. female(susan).
  94. female(linda).
  95.  
  96. parent(john, peter).
  97. parent(john, mary).
  98. parent(mary, susan).
  99. parent(peter, mike).
  100. parent(linda, mike).
  101.  
  102. % Rules
  103. father(F, C) :-
  104. male(F),
  105. parent(F, C).
  106.  
  107. mother(M, C) :-
  108. female(M),
  109. parent(M, C).
  110.  
  111. sibling(X, Y) :-
  112. parent(Z, X),
  113. parent(Z, Y),
  114. X = Y.
  115.  
  116. Practice Exercises:
  117. 1. Add more relationships to the family tree (e.g., grandparent, aunt, uncle).
  118. 2. Write a predicate to find all the children of a given parent.
  119.  
  120. 3. Extend the family tree to include great-grandparent relationships.
  121. 4. Implement a predicate to check if two individuals are cousins.
  122. 5. Modify the family tree to include spouses and write rules to determine marital relationships.
  123. Query Exercises:
  124.  Find all children of John: ?- parent(john, Child).
  125.  List all grandchildren of John: ?- parent(john, Child), parent(Child, Grandchild).
  126.  Find all siblings of Mary: ?- sibling(mary, Sibling).
  127.  Who are the mothers in the family? ?- mother(Mother, _).
  128.  List all uncles in the family: ?- sibling(Uncle, Parent), male(Uncle).
  129.  Find all cousins of Mike: ?- parent(Parent1, Mike), sibling(Parent1, Parent2), parent(Parent2,
  130. Cousin), Cousin = Mike.
  131.  Who are the grandparents in the family? ?- parent(Parent, Person), parent(Parent,
  132. ParentOfParent), Person = ParentOfParent.
  133.  List all females who have children: ?- mother(Female, _).
  134.  Find all siblings and their children: ?- sibling(X, Y), parent(X, Child), format('~w is a sibling of ~w
  135. and has child ~w.~n', [X, Y, Child]), fail.
  136.  List all males in the family with their children: ?- male(Male), parent(Male, Child), format('~w
  137. has child ~w.~n', [Male, Child]), fail.
  138. These queries will allow you to explore the relationships defined in the family tree example and practice
  139. querying in Prolog. Feel free to modify and extend the queries as per your learning objectives.