
















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An excerpt from 'modern programming languages' 2nd edition, focusing on prolog. It covers topics such as instantiation, real values and integers, comparisons, equalities, and the use of predicates like is, abs, sqrt, gcd, and findall. The text also includes examples of prolog code for sum, fact, and subseq, as well as an explanation of how prolog handles search and problem space solutions.
Typology: Slides
1 / 56
This page cannot be seen from the preview
Don't miss anything!

















































Chapter Twenty-Two Modern Programming Languages, 2nd ed. (^) Docsity.com 1
Chapter Twenty-Two Modern Programming Languages, 2nd ed. (^) Docsity.com 2
Chapter Twenty-Two Modern Programming Languages, 2nd ed. 4
Docsity.com
Chapter Twenty-Two Modern Programming Languages, 2nd ed. 5
?- Y is X+2, X=1. ERROR: is/2: Arguments are not sufficiently instantiated ?- X=1, Y is X+2. X = 1, Y = 3.
Docsity.com
Chapter Twenty-Two Modern Programming Languages, 2nd ed. 7
?- X is 1/2. X = 0.5.
?- X is 1.0/2.0. X = 0.5.
?- X is 2/1. X = 2.
?- X is 2.0/1.0. X = 2.0.
Docsity.com
Chapter Twenty-Two Modern Programming Languages, 2nd ed. (^) Docsity.com 8
Chapter Twenty-Two Modern Programming Languages, 2nd ed. (^) Docsity.com 10
Chapter Twenty-Two Modern Programming Languages, 2nd ed. 11
mylength([],0). mylength([_|Tail], Len) :- mylength(Tail, TailLen), Len is TailLen + 1.
?- mylength([a,b,c],X). X = 3.
?- mylength(X,3). X = [_G266, _G269, _G272].
Docsity.com
Chapter Twenty-Two Modern Programming Languages, 2nd ed. 13
sum([],0). sum([Head|Tail],X) :- sum(Tail,TailSum), X is Head + TailSum.
?- sum([1,2,3],X). X = 6.
?- sum([1,2.5,3],X). X = 6.5.
Docsity.com
Chapter Twenty-Two Modern Programming Languages, 2nd ed. 14
gcd(X,Y,Z) :- X =:= Y, Z is X. gcd(X,Y,Denom) :- X < Y, NewY is Y - X, gcd(X,NewY,Denom). gcd(X,Y,Denom) :- X > Y, NewX is X - Y, gcd(NewX,Y,Denom).
Docsity.com
Chapter Twenty-Two Modern Programming Languages, 2nd ed. 16
gcd(X,Y,Z) :- X =:= Y, Z is X, !. gcd(X,Y,Denom) :- X < Y, NewY is Y - X, gcd(X,NewY,Denom), !. gcd(X,Y,Denom) :- X > Y, NewX is X - Y, gcd(NewX,Y,Denom).
Docsity.com
Chapter Twenty-Two Modern Programming Languages, 2nd ed. 17
fact(X,1) :- X =:= 1, !. fact(X,Fact) :- X > 1, NewX is X - 1, fact(NewX,NF), Fact is X * NF.
?- fact(5,X). X = 120.
?- fact(20,X). X = 2432902008176640000.
?- fact(-2,X). false.
Docsity.com
Chapter Twenty-Two Modern Programming Languages, 2nd ed. (^) Docsity.com 19
Chapter Twenty-Two Modern Programming Languages, 2nd ed. 20
Item Weight in kilograms Calories bread 4 9200 pasta 2 4600 peanut butter 1 6700 baby food 3 6900
Docsity.com