1

TOPICS
I. Introduction II. Programs

PAGE NO.
3-7 8-32 8 9

1. Program to find the last element of a list 2. Program to delete an element form a list.

3. Program to demonstrate the example of Green Cut.
4. Program to calculate integer division. 5. Program to check the number of parents. 6. Program to add two numbers.

11 13 14 15

7. Program to subtract one number from another number. 16
8. Program to multiply two numbers.

17 18

9. Program to find the quotient of a number divided by another number.

2

TOPICS

PAGE NO.
20

10. Program to check which number is greater.

11. Program to calculate the factorial of a number. 22 12. Program to find prime numbers up to 20 13. Program to generate Fibonacci number. 14. Program to calculate the area of a Triangle. 15. Program to display the head of a list. 16. Program to display the tail of a list

23-24 25 29-30 31 32

Prolog
Prolog is a logic programming language. Prolog (PROgramming LOGic) rose within the realm of Artificial Intelligence (AI). It is a general

3

purpose language often associated with artificial intelligence and computational linguistics. It has a purely logical subset, called "pure Prolog", as well as a number of extra logical features. It originally became popular with AI researchers, who know more about "what" and "how" intelligent behavior is achieved. The philosophy behind it deals with the logical and declarative aspects. Prolog represents a fundamentally new approach to computing and became a serious competitor to LISP

Significant Language Features

Prolog is a rich collection of data structures in the language and human reasoning, and a powerful notation for encoding end-user applications. It has its logical and declarative aspects, interpretive nature, compactness, and inherent modularity.
• • •

Intelligent Systems - programs which perform useful tasks by utilizing artificaial intelligence techniques. Expert Systems - intelligent systems which reproduce decisionmaking at the level of a human expert. Natural Language Systems - which can analys and respond to statements made in ordinary language as opposed to approved keywords or menu selections. Relational Database Systems

Areas of Application
Prolog is the highest level general-purpose language widely used today. It is taught with a strong declarative emphasis on thinking of the logical relations between objects or entities relevant to a given problem, rather than on procedural steps necessary to solve it. The system decides the way to solve the problem, including the sequences of instructions that the computer must go through to solve it. It is easier to say what we want done and leave it to the computer to do it for us. Since a major criterion in the

4

commercial world today is speed of performance, Prolog is an ideal prototyping language. Its concept makes use of parallel architectures. It solves problems by searching a knowledge base (or more correctly a database) which would be greatly improved if several processors are made to search different parts of the database

The Prolog system recognizes the type of a term object by its syntax. A variable always starts with an uppercase character and all other data types start with a lowercase character. Below there is a picture showing the hierarchy of types in Prolog:

Atoms
Atoms are strings that consist of:
• •

Alphanumeric characters including the underscore character ( _ ) and that start with a lowercase character (A..Z, a..z, _). Atoms enclosed in single or double quotes, for instance 'Papa bear' or "Papa bear". By using quotes an atom can start with a capital and can contain whitespace characters. Inserting a single double quote in a string that starts with a double quote is done by placing two double quotes inside the string, for instance, "This string ""contains"" 2 extra double quotes". The two double quotes inside the string are interpreted by the compiler as a single double quote. This is the same for inserting a single quote inside a string that is enclosed by single quotes. There is however a difference in using the single quote

5

character and the double quote character, single quotes are removed from the atom and double quotes not. For example; the goal 'a' = a. succeeds because the single quotes are removed from the atom 'a'. If the goal is "a"=a. then the result will be no because the double quotes are not removed. There is also the Prolog double_quotes, this Prolog flag determines how the compiler processes atoms enclosed by double quotes. An atom may also consist of the following special characters # \$%&*+-./:<=>@\^`~

Numbers
These are integer numbers like, 33, -33, 0, 23534 and floating point numbers like, 23.3e+34, -0.3532e-03.

Variables
Variables start with an uppercase character and then consist of alphanumeric characters or the underscore character. Below there are some examples of variables: Var X Hello_this_is_a_variable A variable that just consists of a single underscore characters is called an anonymous variable.

Structures
Structures are data objects that consist of one or more other components, these can in turn also be structures. These components are bounded by a functor. A functor is an atom that binds the components. Below there are several examples of structures: mother(clair, jil). point(12, 2). functor_1( g(h, i), other_compound_term(A, b) ).

6

The arity of a structure is the number of components, the arity of point(12, 2) is 2 and the arity of triangle( 12, 2, 15) is 3. Turbo Prolog is a quite old Prolog system that only works with MSDOS. This implies serious memory limitations. Also, it uses a special Prolog dialect with typed variables and some other restrictions that does not comply with the Edinborough Standard . However, it is very fast, and contains a convenient visual debugger. I once learned Prolog by observing this debugger.

PROGRAM 1 - Program to find the last element of a list. Code
Program to find the last element of a list. last(X, [X]). last(X, [_|Y]):- last(X, Y).

7 ?- last(X, [i, love, my, country]), write(X).

Output

PROGRAM 2 - Program to delete an element form a list Code
/* Program to delete an element form a list.*/ efface(A, [A|L], L). efface(A, [B|L], [B|M]):- efface(A, L, M).

8 ?- efface(c, [a, b, c, d, e], R), write(R).

Output

PROGRAM 4 - Program to demonstrate the example of Green Cut Code
/*Program to demonstrate the example of Green Cut.*/ uniform(X, blue):- class(X, Y), Y < 6. uniform(X, red):- class(X, Y), Y > 5, Y < 10. uniform(X, white):- class(X, Y), Y > 9.

9 happy(Child):- uniform(Child, Colour),!,likes(Child, Colour). likes(ramesh, red). class(ramesh, 4). ?- happy(ramesh).

Output

PROGRAM 6 - Program to calculate integer division

Code

10

/*Program to calculate integer division.*/ is_integer(0). is_integer(X):- is_integer(Y), X is Y + 1. divide(Nl, N2, Result) :- is_integer(Result), Product1 is Result*N2,Product2 is (Result+l)*N2, Product1 =< Nl,Product2 > Nl,! . ?- divide(27, 6, X), write(X).

Output

PROGRAM 7 - Program to check the number of parents

Code

11 /* Program to check the number of parents.*/ number_of_parents(adam,N) :- !, N = 0. number_of_parents(eve,N) :- !, N = 0. number_of_parents(X,2). ?- number_of_parents(eve,X), write(X).

Output

PROGRAM 8 - Program to add two numbers

Code
/* Program to add two numbers. */

12

add(X, Y, Z):- Z is X + Y. ?- add(1, 2, S), write(S).

Output

PROGRAM 9 - Program to subtract one number from another
number.

Code
/* Program to subtract one number from another number.*/

13

sub(X, Y, Z):- Z is X - Y. ?- sub(2, 1, S), write(S).

Output

PROGRAM 10 - Program to multiply two numbers. Code
/* Program to multiply two numbers.*/

14 prod(X, Y, Z):- Z is X * Y. ?- prod(3, 2, S), write(S).

Output

PROGRAM 11 - Program to find the quotient of a number divided
by another number

Code
/* Program to find the quotient of a number divided by another number.*/

15

quo(X, Y, Z):- Z is X / Y. ?- quo(6, 2, S), write(S).

Output

PROGRAM 13 Code

Program to check which number is greater.

/* Program to check which number is greater. */ greater(X, Y) :- X > Y, write(X), !. greater(X, Y) :- X < Y, write(Y).

16 ?- greater(3,4).

Output

PROGRAM 15 Code

Program to calculate the factorial of a number.

/* Program to calculate the factorial of a number.*/ fact(1,1). fact(N,F):- N1 is N - 1, fact(N1,F1), F is N * F1.

17 ?-fact(5,R),write(R).

Output

PROGRAM 16 Code

Program to find prime numbers up to 20

/* Program to find prime numbers up to 20.*/ primes(Limit,Ps):- integers(2,Limit,Is),

18 sift(Is,Ps). integers(Low,High,[Low|Rest]):- Low =< High, !, M is Low + 1, integers(M,High,Rest). integers(_,_,[]). sift([],[]). sift([I|Is],[I|Ps]):- remove(I,Is,New), sift(New,Ps). remove(P,[],[]). remove(P,[I|Is],[I|Nis]):- not(0 is I mod P), !, remove(P,Is,Nis). remove(P,[I|Is],Nis):- 0 is I mod P, !, remove(P,Is,Nis). ?-primes(20,Ps),write(Ps).

Output

19

PROGRAM 17 Code

Program to generate Fibonacci number

/* Program to generate Fibonacci number.*/

20 fibo(2,1) :- !. fibo(1,1) :- !. fibo(N,F) :- N1 is N - 1, N2 is N - 2, fibo(N1,F1), fibo(N2,F2), F is F1 + F2. ?-fibo(7,F),write(F).

Output

21

PROGRAM 21 Code

Program to display the head of a list

Output

22

PROGRAM 22 Code

Program to display the tail of a list

/* Program to display the tail of a list.*/ tail([X|Y], Y). ?-tail([abhishek, 2, maiti,1985],T), write(T).

Output

23