0% found this document useful (0 votes)
51 views

Session-5: Arithmetic Operations String Operations

1) Arithmetic and comparison operators in Prolog can function as either assignment operators or comparison operators depending on whether the operands are bound variables or objects. 2) Common string operations in Prolog include concatenation using concat/3, determining string length with str_len/2, and extracting substrings or characters from the front using predicates like frontstr/4 and frontchar/3. 3) Other useful string predicates include isname/1 for testing string syntax, upper_lower/2 for case conversion, and fronttoken/3 for extracting tokens.

Uploaded by

Arun Sai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Session-5: Arithmetic Operations String Operations

1) Arithmetic and comparison operators in Prolog can function as either assignment operators or comparison operators depending on whether the operands are bound variables or objects. 2) Common string operations in Prolog include concatenation using concat/3, determining string length with str_len/2, and extracting substrings or characters from the front using predicates like frontstr/4 and frontchar/3. 3) Other useful string predicates include isname/1 for testing string syntax, upper_lower/2 for case conversion, and fronttoken/3 for extracting tokens.

Uploaded by

Arun Sai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

SESSION-5

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

Turbo Prolog Strings:


A turbo prolog object can be any of six
types: character, string, real, integer,
symbol or file. The string type is any
sequence of characters enclosed
within quotation marks, such as
"John Smith"
"123"
"&fgh"
A string is really a list of characters. Some basic aspects
of strings are listed here.
A string is a list of particular characters. Peter is not
the same as peter.
The order of the characters in a string is significant.
XYZ is not the same as YXZ.
Strings can be of any length.
Strings can be empty, or null. Such strings are specified
by a pair of quotation marks with nothing between
them;
for example
write(" ").
The write predicate succeeds, but does nothing. A null
string is different from a blank space. A string with a
blank space writes a space to the display.
String Operations:
Concatenation:
The concat built-in predicate joins two strings to form a
third. Its general form is:
concat(String 1, String 2, String 3).
where String 3 is concatenation of String 1 and String 2.
Here is a short example:
go:-
X=The quick brown fox,
Y=jumped over the lazy dogs back.,
concat(X, Y, Output),
write(Output).
This program produces
This quick brown for jumped over the lazy dogs
back.
You can also use the concat predicate to return a substring if
you know the output string and one of the input strings.
For example
Go:-
Z=The quick brown fox,
X=The
Concat(X,Y,Z),
Write(Y)
Produces
quick brown fox
The frontstr Predicate

The frontstr predicate extracts NoOfCharacters from the front of


a string
frontstr(NoOfCharacters,InputString,SelectedString,RemainderSt
ring)
where
InputString = Input String of characters(string).
NoOfCharacters = number of left characters to extract (Integer).
SelectedString = extracted string(string).
RemainderString = remainder of input string after extraction.
For Example,
go:-
X=The quick brown fox,
frontstr(3,X,Selected,Rest),
write(Seleced),nl,
write(Rest),nl.
Returns
The
quick brown fox
Determining String Length

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.

The general form of the fronttoken predicate is


fronttoken(InputString, Token, RemainderString)
where
InputString= the input string(string).
Token= the first token in the input string(string).
RemainderString=input string after token is extracted (string).
For example, the program
go:-
X="The quick brown fox",
fronttoken(X, Selected, Rest),
write(Selected), nl,
write(Reset), nl.

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

You might also like