Академический Документы
Профессиональный Документы
Культура Документы
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.
11 13 14 15
17 18
TOPICS
PAGE NO.
20
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
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
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
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
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) ).
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.
Output
Output
Output
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
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
Code
/* Program to add two numbers. */
12
Output
Code
/* Program to subtract one number from another number.*/
13
Output
Output
Code
/* Program to find the quotient of a number divided by another number.*/
15
Output
PROGRAM 13 Code
/* 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
17 ?-fact(5,R),write(R).
Output
PROGRAM 16 Code
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
Output
21
PROGRAM 21 Code
/* Program to display the head of a list.*/ head([X|Y], X). ?-head([abhishek, 2, maiti,1985],H), write(H).
Output
22
PROGRAM 22 Code
/* Program to display the tail of a list.*/ tail([X|Y], Y). ?-tail([abhishek, 2, maiti,1985],T), write(T).
Output
23