DATA TYPES IN PROLOG
- Atom
- Structure
- Variables
Atom
· a string atom, like 'This is a string' or
· a symbol, like likes, john, and pizza, in likes(john, pizza). Atoms of this type must start with a lower case letter. They can include digits (after the initial lower-case letter) and the underscore character (_).
· strings of special characters, like <--->, ..., ===>. When using atoms of this type, some care is needed to avoid using strings of special characters with a predefined meaning, like the neck symbol :-, the cut symbol !, and various arithmetic and comparision operators.
The available special characters for constructing this class of atom are: +, -, *, /, <, >, =, :, ., &, _, and ~.
The available special characters for constructing this class of atom are: +, -, *, /, <, >, =, :, ., &, _, and ~.
Numbers in Prolog, are not considered to be atoms. Numbers used in prolog are integers and real numbers.
Variables:
A variable in Prolog is a string of letters, digits, and underscores (_) beginning either with a capital letter or with an underscore. Examples:
X, Sister, _, _thing, _y47, First_name, Z2
The variable _ is used as a "don't-care" variable, when we don't mind what value the variable has. For example:
I
s_a_parent(X) :- father_of(X, _).
is_a_parent(X) :- mother_of(X, _).
is_a_parent(X) :- mother_of(X, _).
An underscore '_' also known as anonymous variable is used in clauses when a variable need not be inferred to more than once.
That is, X is a parent if they are a father or a mother, but we don't need to know who they are the father or mother of, to establish that they are a parent.
Structures
Structures in Prolog are simply objects that have several components, but are treated as a single object. Suppose that we wish to represent a date in Prolog - dates are usually expressed using a day, a month, and a year, but viewed as a single object.
For example:
date(21, mar, 2004)
Structures may be nested, too - the following example groups a name and a date, perhaps the person's date of birth:
persondata(name(smith, john), date(28, feb, 1963))
List
A list in Prolog is written as a comma-separated sequence of items, between square brackets. For example, [1, 2, 3] is a list.
The empty list is written [].
A list with just a single item, say the number 7, is written [7].
Annaaaa
|
Jack
|
Mya
amm
|
Vkeyt
|
[ ]
|
Arithmetic Operators
SYMBOLS OPERATION
+ Addition
|
- Subtraction
|
* Multiplication
|
/ Real division
|
// Integer division
|
mod Modulus
|
** Power
|
Q1. Write a program to find the factorial of any number. Test it with 11.
PREDICATES
factorial(integer,integer)
CLAUSES
factorial(0,1).
factorial(N,F):-N>0, N1=N-1,factorial(N1,F1),F=N*F1.
GOAL
factorial(11,F).
Output:
Q2. Write a program to find the Fibonacci series of the number being entered. Test it for number 10.
PREDICATES
fibonacii(integer,integer)
CLAUSES
fibonacii(0,1).
fibonacii(1,1).
fibonacii(N,F):-N>1,N1=N-1,N2=N- 2,fibonacii(N1,F1),fibonacii(N2,F2),F=F1+F2.
GOAL
fibonacii(10,F).
Output:
Q3. Define a predicate to output the values of the squares of the integer from N1 to N2 inclusive and test it with N1=6 and N2=12.
PREDICATES
testloop(integer)
CLAUSES
testloop(5).
testloop(N):-N>=6,write("square of "),write(N),write(" is "),N1=N*N,write(N1),nl,M=N-1,testloop(M).
GOAL
testloop(12).
Output:
CONCLUSION/DISCUSSION:
Thus using visual Prolog program the given programs were tested and verified successfully.
No comments:
Post a Comment