Session-5: Arithmetic Operations String Operations
Session-5: Arithmetic Operations String Operations
Arithmetic Operations
String Operations
Arithmetic Operations
go:-
X=4+3,
write(X),nl.
Now compile and execute this program, specifying go
as a goal. You will get the following result:
Goal: go
7
True
Goal:
The first premise binds the variable X to the sum of 4 and
3. The next premise displays the value of the variable. So
far, there seems to be nothing unusual.
go:-
X=4+3,
write(X),nl,
X=4-3,
write(X),nl.
Try to compile and execute this. You will see the following:
Goal: go
7
False
Goal:
The Equal Operator:
The equal sign is the operator. The values to the left
and right are the operands. In Turbo Prolog, the equal
sign can function in either of two ways: If either
operand is a variable that is not bound, the equal
operator functions much like an assignment operator,
binding the variable.
Otherwise, the equal operator functions as a comparison
operator, and a test is performed.
Note: The function of the equal sign varies in different
implementations of Prolog. Some Prologs use an
is operator as an assignment operator.
Five basic rules govern the equal sign in Turbo Prolog.
1. If one operand is a free variable and the other is an object, the
variable will become bound to the object value, and the goal
will succeed.
For example, if X=4+3, X will be bound to 7.
2. If one operand is a bound variable and the other is an object,
the goal will succeed only if the variable is already bound to
the value of the object.
For example, if X is already bound to 7 and X=5+2 is
assessed, the program will display
True
because 5+2 does equal the bound X variable.
3.If one operand is a free variable and the other is a bound
variable, the free variable will be bound to the same value
as the bound variable, and the goal will succeed. For
example, if Y has been bound to 7 X=Y will bind X to 7.
4.If both variables are bound variables, the goal will
succeed only if both are bound to the same value. For
example, if X is bound to 7 and so is Y, then X=Y
will be true; otherwise it will be false.
5.If both operands are objects, the goal will succeed
only if they are the same. For example apple=apple
will succeed.
The comparison operators:
Comparison operators permit a program to compare
bound variables or objects in a test.
The equal sign, as mentioned, can function as an
assignment operator or a comparison operator,
depending upon the variable binding. When you use
comparison operators, all variables must be bound.
go:-
X=2,
X<=4+3,
write(X),nl.
This program will compile. The first premise binds a
value to the variable, and the next tests this value.
The goal will succeed.
Goal: go
2
True
Goal:
You can also use comparison operators with character
objects or variables, string objects or variables, and
symbol objects or variable. A letter is less than another
if it comes earlier in the alphabet.
following are true:
'X'<'Z'
"bob"<"harold"
"walter">"tom
Symbol Meaning
< Less than
> Greater than
= Equal to
<= Less than or equal to
>= Greater than or equal to
<> Not equal to
Using Arithmetic Predicates:
plus(X, Y, Sum):-
Sum=X+Y.
Only works if X and Y are bound and Sum is free. This
program could be expanded as follows:
Plus(X, Y, Sum):-
free(Sum),
Sum=X+Y.
Plus(X, Y, Sum):-
free(X),
X=Sum-Y.
Counters:
Counters must be implemented differently in Prolog
than in other languages. For example, the expression
will not work to implement a counter:
X=X+1
In Prolog you cannot use a free variable on both sides of
the equal sign. On the other hand, if X is already
bound, the test will fail.
The proper way to implement a counter, as mentioned.
Count(9).
count(X):-
Y=X+1,
count(Y).
You can create a delay loop using the following type of
counter:
delay(0):-!.
delay(X):-
Y=X-1,
delay(Y).
For example, if the premise
delay(10)
is invoked, it unifies with the second clause, which
invokes delay(9). This winds downward until
delay(0) is reached. This unifies with the first clause,
terminating the recursion.
Type conversions:
Turbo Prolog provides several predicates for type
conversion:
char_ascii : Converts character to integer or
integer to character if either is bound.
If both are bound, this predicate tests
whether Character is the conversion of Integer.
str_char: Converts String to Character or
Character to String or test.
str_int: Converts String to Integer or Integer to String
or tests.
Str_real: Converts String to Real or Real to String or
tests.
In all of the type conversion predicates,
Character is a character type object.
Integer is an integer.
String is a string(in str_char, it must be a one
character string).
Real is a real number.
String Operations
You can use the str_len predicate to obtain the length of a string.
Its general form is
str_len(InputString,Len)
where
InputString= input string(string).
Len = length of input string(integer).
for example:
go:-
A=cat,
str_len(A,L),
write(L).
The result is: 3
The frontchar Predicate
The frontchar predicate is somewhat similar to the
frontstr predicate, except that it only returns or tests
the first character of the input string. Its general form
is
frontchar(InputString, FrontChar,RemainderString)
where
InputString = the input string(string).
FrontChar = the first character of the input string(char).
RemainderString= the input string after the front
character is extracted
If FrontChar is bound, the predicate succeeds, and it is
bound to the first character of InputString. If
FrontChar is free, it binds to the first character of
InputString.
go:-
I=ROSE,
frontchar(I, F,R),
write(F),nl,write(R).
Result
R
OSE
The fronttoken Predicate:
The fronttoken predicate permits the extraction of tokens from a
string. A token is a name or number , or any nonspace character.
returns
The
quick brown fox
The isname Predicate
The isname Predicate is used to test whether a string
conforms to the Turbo Prolog string syntax. The name
must start with an alphabetic character and be
followed by any number of letters, digits, or
underscores. The predicate's general form is.
Isname(String)
Where String is the string to be tested. The argument
String must be bound, and the predicate returns a value
of true or false.
The upper_lower Predicate:
The upper_lower predicate can be used to convert
uppercase characters to lowercase or lowercase letters
to uppercase. The general form of the predicate is
upper_lower(UpperCaseString, LowerCaseString)
for example:
go:-
A=CAT,
upper_lower(A,B), write(B).
The result is:
cat.
.
If LowerCaseString is bound and UpperCaseString is
free, the predicate returns the uppercase equivalent of
LowerCaseString as UpperCaseString.
You can also bind UpperCaseString and return the
lowercase equivalent as LowerCaseString.
If both arguments are bound, the predicate tests
whether the first argument s the uppercase equivalent
of the second
END OF SESSION-5