Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- male(james1).
- male(charles1).
- male(charles2).
- male(james2).
- male(george1).
- female(catherine).
- female(elizabeth).
- female(sophia).
- parent(charles1, james1).
- parent(elizabeth, james1).
- parent(charles2, charles1).
- parent(catherine, charles1).
- parent(james2, charles1).
- parent(sophia, elizabeth).
- parent(george1, sophia).
- mother(M,X):- parent(X,M),female(M),write(M),write(' is Mother of '),write(X),nl.
- father(F,X):- parent(X,F),male(F).
- sibling(S1,S2):- parent(S1,X), parent(S2,X).
- grandfather(G,X) :- parent(Y,G),parent(X,Y).
- Exercise -
- Implementation:
- Write a Prolog predicate reverse_list/2 that works like the built-in predicate
- reverse/2 (without using reverse/2).
- Program:
- % Base case: Reversing an empty list results in an empty list. reverse_list([], []).
- % Recursive case: To reverse a non-empty list, append the reversed tail to the head.
- reverse_list([Head|Tail], Reversed) :-
- reverse_list(Tail, ReversedTail),
- append(ReversedTail, [Head], Reversed).
- Write a Prolog predicate distance/3 to calculate the distance between two points in
- the 2-dimensional plane. Points are given as pairs of coordinates.
- Program:
- distance((X1, Y1), (X2, Y2), Distance) :-
- Distance is sqrt((X2 - X1) ** 2 + (Y2 - Y1) ** 2).
- Write a prolog program to solve Tower of hanoi Problem. Program:
- % Predicate to solve Tower of Hanoi problem hanoi(N) :-
- move(N, left, middle, right).
- % Predicate to move disks
- move(1, Source, _, Destination) :-
- write('Move top disk from '),
- write(Source),
- write(' to '),
- write(Destination), nl.
- move(N, Source, Auxiliary, Destination) :-
- N > 1,
- M is N - 1,
- move(M, Source, Destination, Auxiliary),
- move(1, Source, _, Destination),
- move(M, Auxiliary, Source, Destination).
- Write a Prolog predicate fibonacci/2 to compute the nth Fibonacci number.
- Program: fibonacci(0, 0).
- fibonacci(1, 1).
- fibonacci(N, Fib) :-
- N > 1,
- N1 is N - 1,
- N2 is N - 2,
- fibonacci(N1, Fib1),
- fibonacci(N2, Fib2),
- Fib is Fib1 + Fib2.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement