Visual Prolog: String Handling & Predicates

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Visual Prolog's string-handling predicates are categorized into basic string manipulation and advanced string manipulation.

False (B)

The frontchar predicate can only be used to split a string into its first character and the remaining substring.

False (B)

The frontchar predicate will always succeed, regardless of the input string.

False (B)

The fronttoken predicate can only divide a string into lexical tokens.

<p>True (A)</p> Signup and view all the answers

Given the goal fronttoken("Visual Prolog", T, R), T will be bound to Visual and R will be bound to Prolog.

<p>True (A)</p> Signup and view all the answers

The frontstr predicate requires the first and second parameters to be unbound when called.

<p>False (B)</p> Signup and view all the answers

The concat predicate is non-deterministic, meaning it can provide multiple solutions.

<p>False (B)</p> Signup and view all the answers

The predicate str_len can only be used to verify the length of a string.

<p>False (B)</p> Signup and view all the answers

The isname predicate checks if a string is a valid Visual Prolog variable name, allowing only digits and underscores.

<p>False (B)</p> Signup and view all the answers

The subchar predicate returns a character at a given position in a string and the positions are zero-indexed.

<p>False (B)</p> Signup and view all the answers

The substring predicate will execute successfully even if the specified position and length result in accessing characters outside the string's boundaries.

<p>False (B)</p> Signup and view all the answers

searchchar will find all occurrences of a character in a given string through backtracking.

<p>False (B)</p> Signup and view all the answers

searchstring can find multiple occurrences of a substring within a source string through backtracking.

<p>False (B)</p> Signup and view all the answers

The predicates for type conversion in Visual Prolog include char_int, str_char, str_int, str_real, and upper_lower.

<p>True (A)</p> Signup and view all the answers

The char_int predicate cannot be used to verify if a character corresponds to a specific integer value.

<p>False (B)</p> Signup and view all the answers

The str_char predicate converts a string to its character representation.

<p>True (A)</p> Signup and view all the answers

The str_int predicate is used exclusively for converting strings to integers; it cannot perform the reverse conversion.

<p>False (B)</p> Signup and view all the answers

The str_real predicate is used to convert strings to floating point numbers

<p>True (A)</p> Signup and view all the answers

The upper_lower predicate is designed to perform case conversions on strings or characters.

<p>True (A)</p> Signup and view all the answers

The upper_lower predicate will always fail if the two strings are the same, even if they have differences in case.

<p>False (B)</p> Signup and view all the answers

Flashcards

String Processing

Standard predicates for powerful string manipulations in Visual Prolog.

Basic String-Handling Predicates

Predicates that serve purposes such as dividing strings, building strings, verifying composition, returning substrings, finding length, creating blank strings and verifying Visual Prolog names.

frontchar/3

Divides a string into its first character and the remaining substring.

fronttoken/3

Splits a string into the first token and the rest of the string, based on Visual Prolog syntax.

Signup and view all the flashcards

frontstr/4

Splits a string into two parts based on a specified number of characters.

Signup and view all the flashcards

concat/3

States that String3 is the string obtained by joining String1 and String2.

Signup and view all the flashcards

str_len/2

Returns or verifies the length of a string, or creates a string of blank spaces of a specified length.

Signup and view all the flashcards

isname/1

Verifies if a string is a valid name according to Visual Prolog's syntax rules.

Signup and view all the flashcards

subchar/3

Returns the character at a specific position in a string.

Signup and view all the flashcards

substring/4

Returns a substring of a specified length starting at a given position.

Signup and view all the flashcards

searchchar/3

Returns the position of the first occurence of a specified character in a string.

Signup and view all the flashcards

searchstring/3

Returns the starting position of the first occurrence of a string within another string.

Signup and view all the flashcards

char_int/2

Converts a character into its ASCII integer value, or vice versa.

Signup and view all the flashcards

str_char/2

Converts a string containing a single character into a character, or vice versa.

Signup and view all the flashcards

str_int/2

Converts a string containing an integer into an integer, or vice versa.

Signup and view all the flashcards

str_real/2

Converts a string containing a real number into a real number, or the reverse.

Signup and view all the flashcards

upper_lower/2

Converts a string to all uppercase or all lowercase.

Signup and view all the flashcards

Study Notes

  • Visual Prolog offers standard predicates for string manipulations, divided into basic string-handling and type conversion.
  • Strings can also be compared.

Basic String-Handling Predicates

  • These predicates form the foundation for string handling in Visual Prolog.
  • They divide a string into components/tokens.
  • They build strings from specified strings/tokens.
  • They verify a string's composition.
  • They return a string, token, or list from a given string.
  • They verify/return the string's length.
  • They create blank strings of a specific length.
  • They verify if a string is a valid Visual Prolog name.

frontchar/3

  • It concatenates Char and String2 to equal String1.
  • Format: frontchar(String1, Char, String2).
  • Example goal: frontchar("ahmed", H, R) results in H = 'a' and R = "hmed".
  • It takes three arguments: a string, a char (first char of the string), and the rest of the string.
  • frontchar splits a string or creates one, and tests characters within.
  • It fails if String1 is a zero-length string.
  • It is used to change a string to a list of characters.
  • Example goal: string_chlist("ABC", Z) binds Z to ['A', 'B', 'C'].

fronttoken/3

  • fronttoken has three functions based on the flow pattern used.
  • Format: fronttoken(String1, Token, Rest).
  • With (i,o,o) flow, it finds the first token of String1, binds it to Token, and the remainder to Rest.
  • The (i,i,o), (i,o,i), and (i,i,i) flow variants are tests.
  • The (o,i,i) variant constructs a string by concatenating Token and Rest, binding String1 to the result.
  • Tokens are grouped as names, numbers or non-space characters based on Visual Prolog Syntax.
  • Suited for decomposing a string into lexical tokens.
  • It divides a sentence into a list of names.

frontstr/4

  • frontstr splits String1 into two parts.
  • Format: frontstr(NumberOfChars, String1, StartStr, EndStr).
  • StartStr contains the first NumberOfChars of String1, and EndStr contains the rest.
  • When called, the first two parameters must be bound, and the last two must be free.

concat/3

  • concat concatenates String1 and String2 to equal String3.
  • Format: concat(String1, String2, String3).
  • At least two parameters must be bound.
  • It gives only one solution.
  • Example: concat("ali", "hits", W) results in W = "alihits".

str_len/2

  • str_len returns or verifies string length, or returns a blank string of a given length.
  • Format: str_len(StringArg, Length).
  • Example: str_len("ali", L) results in L = 3.
  • It binds Length to the length of StringArg or tests if StringArg has the given Length.
  • Length is an unsigned integer.
  • It can return a string of spaces.
  • makebinary is preferable for allocating buffers, especially for binary data.

isname/1

  • isname verifies if the argument is a valid name according to Visual Prolog syntax.
  • Format: isname(String).
  • A name starts with a letter or underscore, followed by letters, digits, and underscores.
  • Spaces are ignored.

subchar/3

  • subchar returns the character at a given position in a string.
  • Format: subchar(String, Position, Char).
  • The first character is at position 1.
  • It exits with an error if the position is beyond the string's end.

substring/4

  • substring returns a part of another string.
  • Format: substring(Str_in, Pos, Len, Str_out).
  • Str_out is bound to the string starting at Pos with length Len from Str_in.
  • It exits with an error if Pos and Len specify a string outside of Str_in.
  • It is not an error to ask for 0 bytes at the extreme end of the string.

searchchar/3

  • searchchar returns the position of the first occurrence of a character in a string.
  • Format: searchchar(String, Char, Position).
  • If the character isn't found, it fails and is not re-satisfiable.

searchstring/3

  • searchstring returns the position of the first occurrence of a string in another string.
  • Format: searchstring(SourceStr, SearchStr, Pos).
  • It fails if the search string isn't found or is longer than the source.
  • It is not re-satisfiable.

Type Conversion Predicates

  • Predicates in this category include: char_int, str_char, str_int, str_real, and upper_lower.
  • They facilitate conversions between different data types and strings.

char_int/2

  • char_int converts a character to an integer or vice versa.
  • Format: char_int(Char, Integer).
  • The arguments must correspond when both are bound. If one is bound, the other is converted and bound.

str_char/2

  • str_char converts a string containing one character into a character or converts a single character into a string of one character.
  • Format: str_char(String, Char).
  • In the (i,i) flow, it succeeds if String is bound to the single character string equivalent of Char, otherwise fails.

str_int/2

  • str_int converts a string containing an integer into an integer, or converts an integer into its textual representation.
  • Format: str_int(String, Integer).
  • With the (i,i) flow, it succeeds if Integer is bound to the integer equivalent of the string.

str_real/2

  • str_real converts a string containing a real number to a real number, or vice versa.
  • Format: str_real(String, Real).
  • With the (i,i) flow, it succeeds if Real is bound to the real equivalent of the real number represented by the string.

upper_lower/2

  • upper_lower converts a string to all lower/upper case.
  • Format: upper_lower(Upper, Lower).
  • It succeeds when both arguments, except for the case of letters, are identical.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

RN Pedia Auditory & Visual Practice Questions
92 questions
Psychology Chapter 10 - Visual Imagery
61 questions
Use Quizgecko on...
Browser
Browser