9 Mayıs 2016 Pazartesi

PROLOG CALISMALARIM #2


LISTS



Swi prolog Windows ta calistirmak icin ['/Users/user_name/Location/ex1.pl']. Ubuntu da calistirmak icin ['/home/user_name/Location/ex1.pl'].
1) There are given some facts for some German numbers and their English translation.

tran(eins,one).tran(zwei,two).tran(drei,three).tran(vier,four).tran(funf,five).tran(sechs,six).
We should write a predicate listtrans(G,E) where G german number, E english number of that number. For example listtran([eins,neun,zwei],X). should give X = [one,nine,two].

listtrans([],[]). 'The empty list should give empty number'
listtrans([X|T1],[Y|T2]):- tran(X,Y), listtrans(T1,T2).


2) We should duplicate the elements of a given list and return that new list.

For example double([1,a,5]) should return [1,1,a,a,5,5]

double([],[]).
double([A|T1],[A,A|T2]):- double(T1,T2).


3) We should combine elements of two given lists and return that new list.

For example combin([a,b,c],[1,2,3,4],X) should return X=[a,1,b,2,c,3,4]

combin(H1,[],H1).
combin([],H2,H2).
combin([H1|T1],[H2|T2],[H1,H2|T3]) :- combin(T1,T2,T3).

4) Finding the length of a list.

leng([],0).
leng([H|T],X) :- leng(T,Y), X is Y+1.

5) Another way of finding length of a list using Accumulator.

leng1([],Acc,Acc).
leng1([_|T],Acc,L) :- Acc1 is Acc+1, leng1(T,Acc1,L).




Representation Of Some Arithmetic Operations In Prolog



?- 5=:=5.
yes

?- 5=\=5.
false

etc...

6) Maximum element of a list.


maxElement([H|T],A,Max) :- H>A, maxElement(T,H,Max).
maxElement([H|T],A,Max) :- H =< A, maxElement(T,A,Max).
maxElement([],A,A).

7) Add 1 to each element of a list.

add_one([H1|T1],[H2|T2]) :- H2 is H1+1, add_one(T1,T2).
add_one([],[]).

?-add_one([1,2,4,6],X).
X = [2,3,5,7].

8) Scalar Multiplication
For example scalar_mult(3,[2,4,7],Result) should give
Result = [6,12,21]

scalar_mul(A,[],[]).
scalar_mul(A,[H1|T1],[H2|T2]) :- H2 is A*H1, scalar_mul(A,T1,T2).

9) Dot Product
For example dot([2,5,6],[3,4,1],Result) should give
2*3 + 5*4 + 6*1 = 32 Result = 32

dot([],[],0).
dot([H1|T1],[H2|T2],R) :- dot(T1,T2,Result), R is H1*H2+Result.

10) Controlling Palindrome

pal(L) :- reverse(L,L).

?-pal([e,v,e]).
true

11) Write a predicate to control whether given value is second element of list.

second(X,L) :- [_,X|_] = L

12) Write a predicate to control whether given value is last element of list.

last(X,[X]).
last(X,[Y|T]) :- last(X,T).


13) Write a predicate toptail(InList,Outlist) which says ‘no’ if inlist is a list containing fewer than 2 elements, and which deletes the first and the last elements of Inlist and returns the result as Outlist, when Inlist is a list containing at least 2 elements. For example: toptail([a],T). no toptail([a,b],T). T=[] toptail([a,b,c],T). T=[b]

top_tail([_|T],L) :- append(L,[_],T).

?- trace, toptail([a,b,c,d],T).
   Call: (8) top_tail([a, b, c, d], _G1559) ? creep 
   Call: (9) lists:append(_G1559, [_G1892], [b, c, d]) ? creep
   Exit: (9) lists:append([b, c], [d], [b, c, d]) ? creep
   Exit: (8) top_tail([a, b, c, d], [b, c]) ? creep
T = [b, c] .



14) Write a predicate swapfl(List1,List2) which checks whether List1 is identical to List2, except that the first and last elements are exchanged.
For example [a,b,c,d,e] should return [e,b,c,d,a]

iden_ti_cal([H|T],L2) :- append(A,[Y],T), append([Y],A,K), append(K,[H],L2).

15) Write a predicate swap12(List1,List2) which checks whether List1 is identical to List2, except that the first two elements are exchanged.

similar([H|T],L2) :- append([Y],A,T), append([Y],[H],B), append(B,A,L2).

16) There is a street with three neighboring houses that all have a different color. They are red, blue, and green. People of different nationalities live in the different houses and they all have a different pet. Here are some more facts about them: • The Englishman lives in the red house. • The jaguar is the pet of the Spanish family. • The Japanese lives to the right of the snail keeper. • The snail keeper lives to the left of the blue house. • Who keeps the zebra? Define a predicate zebra/1 that tells you the nationality of the owner of the zebra. Hint: Think of a representation for the houses and the street. Code the four constraints in Prolog. member and sublist might be useful predicates.


komsu(A,B,[A,B|_]).
komsu(A,B,[_|T]) :- komsu(A,B,T).

zebra(X) :- Sokak = [E1,E2,E3],
        member(ev(red,english,_),Sokak),
        member(ev(_,spanish,jaguar),Sokak),
        komsu(ev(_,_,snail),ev(_,japanese,_),Sokak),
        komsu(ev(_,_,snail),ev(blue,_,_),Sokak),
        member(ev(_,X,zebra),Sokak).

?-zebra(X).
japanese

16) There is a round table of 5 seats. Women and men seat at the table. Please find whether there are three women sitting next to each other around the table using Prolog.

woman(mary).
woman(alice).
woman(lily).
man(eric).
man(john).
man(ted).

neighbour(X,Y,[X,Y,_,_,_]).
neighbour(X,Y,[_,X,Y,_,_]).
neighbour(X,Y,[_,_,X,Y,_]).
neighbour(X,Y,[_,_,_,X,Y]).
neighbour(X,Y,[Y,_,_,_,X]).

rounded_woman(X) :- Table = [mary,alice,lily,eric,ted],
                    woman(X),
                    woman(Y),
                    neighbour(X,Y,Table),
                    woman(Z),
                    neighbour(Z,X,Table).

?-rounded_woman(W).
W=alice

17) Calculating fibonacci numbers.

1 1 2 3 5 8 13 21 ...

f(1) = 1
f(2) = 1
f(n) = f(n-1) + f(n-2)

fibo(1,1).
fibo(2,1).
fibo(X,Y) :- Z is X-1,
             fibo(Z,T),
             G is X-2,
             fibo(G,V),
             Y is T+V.
?-fibo(7,A).
A = 13

18) Occurrence of an element in list.

occur(_,[],0).
occur(A,[A|T],N) :- occur(A,T,N1), N is N1+1.
occur(A,[H|T],N) :- occur(A,T,N). 

?- occur(2,[2,1,4,2,7,2,3,9,2,2,4,2],P).
P = 6 .

19) Finding the order of an element in list.

order(A,[A|T],1).
order(A,[H|T],B) :- order(A,T,B1), B is B1+1.
?-order(a,[b,c,d,a,e],P).
P = 4


20) Finding the power of a number.

po(G,G,0,1).
po(A,A,C,S) :- po(A,A,C1,S1), C is C1+1, S is S1*A.

pow(A,B,C):- po(A,A,B,C).

?-pow(3,2,L).
L=9.

Towers of Hanoi Puzzle


Here is solition for puzzle.And Here is code for that puzzle.


   move(1,X,Y,_) :-  
    write('Move top disk from '), 
    write(X), 
    write(' to '), 
    write(Y), 
    nl. 
move(N,X,Y,Z) :- 
    N>1, 
    M is N-1, 
    move(M,X,Z,Y), 
    move(1,X,Y,_), 
    move(M,Z,Y,X).
?- ?- move(3,left,right,center).
Move top disk from left to right
Move top disk from left to center
Move top disk from right to center
Move top disk from left to right
Move top disk from center to left
Move top disk from center to right
Move top disk from left to right
true .





1 yorum:

  1. Emeğinize sağlık, başarılı bir anlatım olmuş. Çalışmalarınızın devamını diliyorum :)

    YanıtlaSil