How to make "less than" function in Prolog? - prolog

As i am new to Prolog i made this program but having a lot of troubles can anyone help me?
below is my code:
android(samsung,s10,brown,90000).
android(huwawei,mate10lite,black,34000).
android(oppo,f9,blue,20000).
featured(nokia,1110,brown,10000).
featured(qmobile,q3,black,24000).
featured(gfive,g300,blue,30000).
amount(X,Y,Z,A):-
android(X,Y,Z,A),
A > 25000,
featured(X,Y,Z,A),
A>25000.
fun:-
amount(X,Y,Z,A),
writef("cellphone having less than 25K is",[X],[Y],[Z],[A]),
fail.
i don't know what is the issue if anyone can help please help me out.
when ever i am calling fun it's returning false.

Evidently,
amount(X,Y,Z,A):-
android(X,Y,Z,A), <---+
A > 25000, +--- exactly as for X,Y,Z, this is the SAME A
featured(X,Y,Z,A), <---+
A>25000. <------- ... so you've already tested this!
you really want to have a B:
amount(X,Y,Z,A):-
android(X,Y,Z,A),
A > 25000,
featured(X,Y,Z,B),
B =< 25000.
And why do you fail. at the end of fun? That doesn't sound like fun at all!

Related

Prolog program to search book by book_title ,by book_author, by book_edition using recursion

I am trying, but unable to solve this as I am totally new to Prolog. Anybody can help to solve it?
My Code:
firstBook(the_design_of_everyday_things,don_normas,third_edition).
secondBook(elementary_statistics,a_step_by_atep_approach,fourth_edition).
thirdBook(beginning_linux_programming,neil_methew,third_edition).
Find_book:-
repeat,nl,
write('Book Lists:'),nl,
write('1. Human Computer Interaction'),nl,
write('2. Probability and Statistics'),nl,
write('3. Operating System'),nl,nl,
read(Opt),
{
Opt = 4,
write('Not Available'),!
;
selectedchoice(Opt),
fail
}.

Prolog output message understanding

This is a really quick one, I don't know the correct way to google it!
Say we have an output such it gives:
X = [4|_1218]
what generally has to be changed to get the output just to say 4?
member1(Head, [Head|_]):-!.
member1(Item, [_|Tail]):-
member1(Item, Tail).
findnotin([Head|_], RuledOut, [Head|_]):-
not(member1(Head, RuledOut)).
Edit Completedish program:
find_not_in([], _, _).
find_not_in([Head|Candidates], RuleOut, [Head|Legal]):-
not(member1(Head, RuleOut)),
find_not_in(Candidates, RuleOut, Legal).
find_not_in([_|Candidates], RuleOut, Legal):-
find_not_in(Candidates, RuleOut, Legal).

Prolog Tube Line Task

i have an assignment to make a simplified metro tube map in prolog, one part is asking to make a rule to check if two stations are on the same line, ive got a rule but it doesnt seem to work this is what i have so far:
adjacent(nh,lg,central,4).
adjacent(lg,oc,central,4).
adjacent(oc,tc,central,4).
adjacent(tc,cl,central,4).
adjacent(cl,ls,central,4).
adjacent(ls,bg,central,4).
adjacent(br,vi,victoria,4).
adjacent(vi,oc,victoria,4).
adjacent(oc,ws,victoria,4).
adjacent(ws,kx,victoria,4).
adjacent(kx,fp,victoria,4).
adjacent(ke,em,northern,4).
adjacent(em,tc,northern,4).
adjacent(tc,ws,northern,4).
adjacent(ws,eu,northern,4).
adjacent(al,ls,metropolitan,4).
adjacent(ls,kx,metropolitan,4).
adjacent(kx,bs,metropolitan,4).
adjacent(bs,fr,metropolitan,4).
adjacent(ec,em,bakerloo,4).
adjacent(em,oc,bakerloo,4).
adjacent(oc,pa,bakerloo,4).
adjacent(pa,wa,bakerloo,4).
next(X,Y,L):-adjacent(X,Y,L,_).
next(X,Y,L):-adjacent(Y,X,L,_).
direct_connect(X,Y,L,S,F):-
next(X,Z,L),
not(member(Z,S)),
direct_connect(Z,Y,L,[Z|S],F).
direct_connect(X,Y,L,S,[Y|S]):- next(X,Y,L).
one_change(X,Y,L,F):-
direct_connect(X,Z,L,[X],F1),
direct_connect(Z,Y,L2,[Z|F1],F),
L\=L2.
exist(X):-next(X,_,_).
member(X,[X|_]).
member(X,[_|T]):-member(X,T).
route(X,Y,F):-exist(X),exist(Y),
direct_connect(X,Y,_,[X],F),
write('Direct Connection'),nl,
revwrite(F).
route(X,Y,F):-exist(X),exist(Y),
one_change(X,Y,_,F),
write('One change required'),nl,
revwrite(F).
revwrite([X]):-write(X).
revwrite([H|T]):-revwrite(T), write('->'),write(H).
and the rule;
sameLine(Stat1, Stat2, Line) :-
station(Stat1, Line),
station(Stat2, Line),
Stat1 \= Stat2.
sorry for the unclear and no code propey posted, posting from phone :/

Is the PROLOG knowledge base created suitable for the following statements exercise?

The exercise was as follows:
Represent the following in Prolog:
Butch is a killer.
Mia and Marsellus are married.
Zed is dead.
Marsellus kills everyone who gives Mia a footmassage.
Mia loves everyone who is a good dancer.
Jules eats anything that is nutritious or tasty.
The knowledge base code I wrote was:
killer(Butch).
married(Mia,Marsellus).
dead(Zed).
kills(Marsellus, Y):- getsfootmassagefrom(Mia, Y).
loves(Mia, X):- goodDancer(X).
eats(Jules, X):- nutritious(X); tasty(X).
Is this correct or have any made any errors whilst writing the rules?
Thanks
Is this more like it?
killer(butch).
killer(marcellus).
adult(mia).
adult(marcellus).
adult(jules).
loves(marcellus,mia).
loves(mia,marcellus)
loves(zed,butch).
married(X,Y):-
loves(X,Y),
adult(X).
'good dancer'(butch).
'good dancer'(zed).
'mia loves'(X):-
'good dancer'(X).
'mia footmassager'(zed).
'mia footmassager'(jules).
'mia footmassager'(butch).
'marcellus kills'(X):-
'mia footmassager'(X).
nutritious('cod liver oil').
nutritious(hummus).
tasty(hummus).
tasty(chocolate).
'jules eats'(X):-
nutritious(X),
tasty(X).

Prolog find minimum in list error

Im trying to make my code work, but somehow I got stuck on a problem, Im very newbie to prolog. This is my code.
dist(valmiera, riga, 107).
%dist(riga, valmiera, 107).
dist(cesis, riga, 70).
dist(valmiera, rujiena, 50).
dist(rujiena, valka, 30).
dist(valmiera, strenci, 200).
dist(strenci, valka, 30).
dist(valmiera, cesis, 40).
dist(liepaja, saldus, 100).
dist(saldus, riga, 200).
dist(liepaja, jelgava, 270).
dist(jelgava, riga, 50).
path(A,B,C,[A,B]):- dist(A,B,C).
path(A,B,D,[A|As]):- dist(A,W,C), path(W,B,E,As), D is C+E.
%, findMin(YList, E), path(A,B,X,E),!.
shortestPath(A,B,X,E):-findall(Y,path(A,B,S,Y),YList), findMin(YList, E), path(A,B,X,E),!.
findMin([],fail).
findMin([H],E):-E=H,!.
findMin([H,V],E):-H<V, E=H,!; V<H, E=V, !; H=:=V, E=H, !.
findMin([H|T],E):-findMin(T,U), H<U, E=H,!;findMin(T,U), U<H, E=U,!;findMin(T,U), U=:=H, E=U,!.
but when I call findMin() I get this error
uncaught exception: error(type_error(evaluable,'.'/2),(<)/2)
I'm realy stuck and don't know what to do. Any help will be appreciated.
Applications purpose is get shortest path by calling shortestPath(), paths are in dist (a,b,distance)
The exception is because the terms you are trying to compare are lists.
[liepaja,saldus,riga]<[liepaja,jelgava,riga] ?
An expression:
Term1 < Term2
succeeds if
eval(Term1) < eval(Term2)
So, Term1 and Term2 have to be evaluable terms (e.g., (1+1)<(2+2)).
Try to change the shortestPath/4 body:
shortestPath(A,B,X,E):-
findall(couple(W,P),path(A,B,W,P),WP_List),
findMin(WP_List, couple(Weight,ShortestPath)),...
In this way you have a list of couple(Weight,Path) and in findMin you can get the Weight per Path.

Resources