Rexx – Decision Making ”; Previous Next Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program. The following diagram shows the general form of a typical decision-making structure found in most of the programming languages. There is a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false. Let’s look at the various decision-making statements available in Rexx. Sr.No. Statement & Description 1 If statement The first decision-making statement is the if statement. An if statement consists of a Boolean expression followed by one or more statements. 2 If-else statement The next decision-making statement is the if-else statement. An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. Nested If Statements Sometimes there is a requirement to have multiple if statements embedded inside each other, as is possible in other programming languages. In Rexx also this is possible. Syntax if (condition1) then do #statement1 end else if (condition2) then do #statement2 end Flow Diagram The flow diagram of nested if statements is as follows − Let’s take an example of nested if statement − Example Live Demo /* Main program */ i = 50 if (i < 10) then do say “i is less than 10” end else if (i < 7) then do say “i is less than 7” end else do say “i is greater than 10” end The output of the above program will be − i is greater than 10 Select Statements Rexx offers the select statement which can be used to execute expressions based on the output of the select statement. Syntax The general form of this statement is − select when (condition#1) then statement#1 when (condition#2) then statement#2 otherwise defaultstatement end The general working of this statement is as follows − The select statement has a range of when statements to evaluate different conditions. Each when clause has a different condition which needs to be evaluated and the subsequent statement is executed. The otherwise statement is used to run any default statement if the previous when conditions do not evaluate to true. Flow Diagram The flow diagram of the select statement is as follows The following program is an example of the case statement in Rexx. Example Live Demo /* Main program */ i = 50 select when(i <= 5) then say “i is less than 5” when(i <= 10) then say “i is less than 10” otherwise say “i is greater than 10″ end The output of the above program would be − i is greater than 10 Print Page Previous Next Advertisements ”;
Category: Computer Programming
Rexx – Numbers
Rexx – Numbers ”; Previous Next Rexx has the following data types when it comes to numbers. Integer − A string of numerics that does not contain a decimal point or exponent identifier. The first character can be a plus (+) or minus (-) sign. The number that is represented must be between -2147483648 and 2147483647, inclusive. Big Integer − A string of numbers that does not contain a decimal point or an exponent identifier. The first character can be a plus (+) or minus (-) sign. The number that is represented must be between -9223372036854775808 and 2147483648, inclusive, or between 2147483648 and 9223372036854775807. Decimal − One of the following formats − A string of numerics that contains a decimal point but no exponent identifier, where p represents the precision and s represents the scale of the decimal number that the string represents. The first character can be a plus (+) or minus (-) sign. A string of numerics that does not contain a decimal point or an exponent identifier. The first character can be a plus (+) or minus (-) sign. The number that is represented is less than -9223372036854775808 or greater than 9223372036854775807. Float − A string that represents a number in scientific notation. The string consists of a series of numerics followed by an exponent identifier (an E or e followed by an optional plus (+) or minus (-) sign and a series of numerics). The string can begin with a plus (+) or minus (-) sign. Let’s now look at the different methods available for numbers. Sr.No. Methods available for Numbers 1 ABS This method returns the absolute value of an input number. 2 MAX This method returns the maximum value from a list of numbers. 3 MIN This method returns the minimum value from a list of numbers. 4 RANDOM This method returns a random generated number. 5 SIGN Returns 1 if number is greater than 0, or 0 if the number is 0, or -1 if the number is less than 0. 6 TRUNC This method truncates a number. Print Page Previous Next Advertisements ”;
Rexx – Strings
Rexx – Strings ”; Previous Next Strings in Rexx are denoted by a sequence of characters. The following program is an example of strings − Live Demo /* Main program */ a = “This is a string” say a The output of the above program is as follows − This is a string Let’s discuss some methods which are available in Rexx for strings. Sr.No. Methods available in Rexx for Strings 1 left This method returns a certain number of characters from the left of the string. 2 right This method returns a certain number of characters from the right of the string. 3 length This method returns the number of characters in the string. 4 reverse This method returns the characters in a reverse format. 5 compare This method compares 2 strings. Returns “0” if “string1” and “string2” are identical. Otherwise, it returns the position of the first character that does not match. 6 copies This method copies a string n number of times. 7 substr This method gets a sub string from a particular string. 8 pos This method returns the position of one string within another. 9 delstr This method deletes a sub string from within a string. Print Page Previous Next Advertisements ”;
Rexx – Stacks
Rexx – Stacks ”; Previous Next The stack is sometimes called the external data queue, but we follow common usage and refer to it as the stack. It is a block of memory that is logically external to Rexx. Instructions like push and queue place data into the stack, and instructions like pull and parse pull extract data from it. The queued built-in function reports how many items are in the stack. Let’s take a look at an example of a stack. /* STACK: */ /* */ /* This program shows how to use the Rexx Stack as either a */ /* stack or a queue. */ do j = 1 to 3 push ‘Stack: line #’ || j /* push 3 lines onto the stack */ end do j = 1 to queued() /* retrieve and display LIFO */ pull line say line end do j = 1 to 3 queue ‘Queue: line #’ || j /* queue 3 lines onto the stack */ end do queued() /* retrieve and display FIFO */ pull line say line end exit 0 The first do loop in the program places three lines of data onto the stack. It uses the push instruction to do this. We number the lines so that when they are retrieved in the LIFO order their order is apparent. The items placed into the stack by the push instruction are retrieved in the LIFO order − do j = 1 to 3 push ‘Stack: line #’ || j /* push 3 lines onto the stack */ end The next code block shows the use of the queued built-in function to discover the number of lines on the stack, as well as a loop to retrieve all the lines from the stack − do j = 1 to queued() /* retrieve and display LIFO */ pull line say line end Since the three items were placed on the stack via push, they are retrieved in the LIFO order. The output of the above program will be as follows. STACK: LINE #3 STACK: LINE #2 STACK: LINE #1 Print Page Previous Next Advertisements ”;
Prolog – Quick Guide
Prolog – Quick Guide ”; Previous Next Prolog – Introduction Prolog as the name itself suggests, is the short form of LOGical PROgramming. It is a logical and declarative programming language. Before diving deep into the concepts of Prolog, let us first understand what exactly logical programming is. Logic Programming is one of the Computer Programming Paradigm, in which the program statements express the facts and rules about different problems within a system of formal logic. Here, the rules are written in the form of logical clauses, where head and body are present. For example, H is head and B1, B2, B3 are the elements of the body. Now if we state that “H is true, when B1, B2, B3 all are true”, this is a rule. On the other hand, facts are like the rules, but without any body. So, an example of fact is “H is true”. Some logic programming languages like Datalog or ASP (Answer Set Programming) are known as purely declarative languages. These languages allow statements about what the program should accomplish. There is no such step-by-step instruction on how to perform the task. However, other languages like Prolog, have declarative and also imperative properties. This may also include procedural statements like “To solve the problem H, perform B1, B2 and B3”. Some logic programming languages are given below − ALF (algebraic logic functional programming language). ASP (Answer Set Programming) CycL Datalog FuzzyCLIPS Janus Parlog Prolog Prolog++ ROOP Logic and Functional Programming We will discuss about the differences between Logic programming and the traditional functional programming languages. We can illustrate these two using the below diagram − From this illustration, we can see that in Functional Programming, we have to define the procedures, and the rule how the procedures work. These procedures work step by step to solve one specific problem based on the algorithm. On the other hand, for the Logic Programming, we will provide knowledge base. Using this knowledge base, the machine can find answers to the given questions, which is totally different from functional programming. In functional programming, we have to mention how one problem can be solved, but in logic programming we have to specify for which problem we actually want the solution. Then the logic programming automatically finds a suitable solution that will help us solve that specific problem. Now let us see some more differences below − Functional Programming Logic Programming Functional Programming follows the Von-Neumann Architecture, or uses the sequential steps. Logic Programming uses abstract model, or deals with objects and their relationships. The syntax is actually the sequence of statements like (a, s, I). The syntax is basically the logic formulae (Horn Clauses). The computation takes part by executing the statements sequentially. It computes by deducting the clauses. Logic and controls are mixed together. Logics and controls can be separated. What is Prolog? Prolog or PROgramming in LOGics is a logical and declarative programming language. It is one major example of the fourth generation language that supports the declarative programming paradigm. This is particularly suitable for programs that involve symbolic or non-numeric computation. This is the main reason to use Prolog as the programming language in Artificial Intelligence, where symbol manipulation and inference manipulation are the fundamental tasks. In Prolog, we need not mention the way how one problem can be solved, we just need to mention what the problem is, so that Prolog automatically solves it. However, in Prolog we are supposed to give clues as the solution method. Prolog language basically has three different elements − Facts − The fact is predicate that is true, for example, if we say, “Tom is the son of Jack”, then this is a fact. Rules − Rules are extinctions of facts that contain conditional clauses. To satisfy a rule these conditions should be met. For example, if we define a rule as − grandfather(X, Y) :- father(X, Z), parent(Z, Y) This implies that for X to be the grandfather of Y, Z should be a parent of Y and X should be father of Z. Questions − And to run a prolog program, we need some questions, and those questions can be answered by the given facts and rules. History of Prolog The heritage of prolog includes the research on theorem provers and some other automated deduction system that were developed in 1960s and 1970s. The Inference mechanism of the Prolog is based on Robinson’s Resolution Principle, that was proposed in 1965, and Answer extracting mechanism by Green (1968). These ideas came together forcefully with the advent of linear resolution procedures. The explicit goal-directed linear resolution procedures, gave impetus to the development of a general purpose logic programming system. The first Prolog was the Marseille Prolog based on the work by Colmerauer in the year 1970. The manual of this Marseille Prolog interpreter (Roussel, 1975) was the first detailed description of the Prolog language. Prolog is also considered as a fourth generation programming language supporting the declarative programming paradigm. The well-known Japanese Fifth-Generation Computer Project, that was announced in 1981, adopted Prolog as a development language, and thereby grabbed considerable attention on the language and its capabilities. Some Applications of Prolog Prolog is used in various domains. It plays a vital role in automation system. Following are some other important fields where Prolog is used − Intelligent Database Retrieval Natural Language Understanding Specification Language Machine Learning Robot Planning Automation System Problem Solving Prolog – Environment Setup In this chapter, we will discuss how to install Prolog in our system. Prolog Version
Rexx – Overview
Rexx – Overview ”; Previous Next Rexx (Restructured Extended Executor) is designed to be a scripting language. Its goal is to make scripting as easy, fast, reliable, and error-free as possible. Many programming languages are designed for compatibility with older languages, and are written for specific audiences or platforms. Rexx ignores extraneous objectives. It was designed from day one to be powerful, yet easy to use. Rexx was designed and first implemented, in assembly language, as an ”own-time” project between 20th March 1979 and the middle of 1982 by Mike Cowlishaw of IBM, originally as a scripting programming language to replace the languages EXEC and EXEC 2. It was designed to be a macro or scripting language for any system. As such, Rexx is considered a precursor to Tcl and Python. Rexx was also intended by its creator to be a simplified and easier to learn version of the PL/I programming language. Features of Rexx Rexx as a programming language has the following key features − Simple syntax The ability to route commands to multiple environments The ability to support functions, procedures and commands associated with a specific invoking environment. A built-in stack, with the ability to interoperate with the host stack if there is one. Small instruction set containing just two dozen instructions Freeform syntax Case-insensitive tokens, including variable names Character string basis Dynamic data typing, no declarations No reserved keywords, except in local context No include file facilities Arbitrary numerical precision Decimal arithmetic, floating-point A rich selection of built-in functions, especially string and word processing Automatic storage management Crash protection Content addressable data structures Associative arrays Straightforward access to system commands and facilities Simple error-handling, and built-in tracing and debugger Few artificial limitations Simplified I/O facilities The official website for Rexx is www.oorexx.org Print Page Previous Next Advertisements ”;
Rexx – Arrays
Rexx – Arrays ”; Previous Next Arrays in any programming language allow you to group a list of values of the same type. The use of arrays is that it allows you to build a list of similar type of values which are sortable, searchable and can be easily manipulated. Rexx also allows one to define arrays. These arrays can be one dimensional or multidimensional. Rexx arrays may be sparse. That is, not every array position must have a value or even be initialized. There can be empty array positions, or slots, between those that do contain data elements. Or arrays can be dense, in which consecutive array slots all contain data elements. In many programming languages, you must be concerned with what the subscript of the first entry in a table is. Is the first numeric subscript 0 or 1? In Rexx, the first subscript is whatever you use! So, input the first array element into position 0 or 1 as you prefer. array_name.0 = ‘first element’ or array_name.1 = ‘first element’ Let’s look at the different operations available for arrays. Creating Arrays Arrays are created with the same naming convention which is used for variables in Rexx. The general syntax for creating arrays is as follows − Arrayname.index = value where Arrayname − This is the name provided to the array. Index − This is the index position in the array to refer to a specific element. Value − This is the value assigned to the index element in the array. An example of an array declaration is as follows − Example /* Main program */ list.1 = 0 list.2 = 0 list.3 = 0 The following points needs to be noted about the above program − The name of the array is given as list There are 3 elements of the array which are initialized to the value of 0. Assigning Values to an Array Element Values can be re-assigned to array elements in the same way as array elements are initialized. The following program is an example of values which can be assigned to various index values of an existing array. /* Main program */ list.1 = 0 list.2 = 0 list.3 = 0 /* Assigning new values to the array*/ list.1 = 10 list.3 = 30 Displaying Values of an Array The values of an array can be displayed by referring to the index position of the array element. The following example shows to access various elements of the array. Example Live Demo /* Main program */ list.1 = 0 list.2 = 0 list.3 = 0 /* Assigning new values to the array*/ list.1 = 10 list.3 = 30 say list.1 say list.2 say list.3 The output of the above program will be as follows − 10 0 30 Copying Arrays All of the elements of an array can be copied onto another array. The general syntax of this is as follows − Newarray. = sourcearray. where Newarray − This is the new array in which the elements need to be copied onto. Sourcearray − This is the source array from which the elements need to be copied. An example on how the copy operations for arrays can be carried out is shown in the following program − Example Live Demo /* Main program */ list.1 = 0 list.2 = 0 list.3 = 0 /* Assigning new values to the array*/ list.1 = 10 list.3 = 30 listnew. = list. say listnew.1 say listnew.2 say listnew.3 The output of the above program will be − 10 0 30 Iterating through array elements Elements of an array can also be iterated by using the iterative statements available in Rexx. An example on how this can be done is as follows − Example Live Demo /* Main program */ list.1 = 10 list.2 = 20 list.3 = 30 number_of_elements = 3 do j = 1 to number_of_elements say list.j end The following pointers need to be noted about the above program − The do loop is used to iterate through the array elements. The variable number_of_elements is used to store the number of elements in the array. The variable j is used to iterate through each element of the array. The output of the above program will be − 10 20 30 Two-dimensional Arrays It was also mentioned that we can construct multi-dimensional arrays in Rexx. Let’s look at an example of how we can implement a 2-dimensional array. Example Live Demo /* Main program */ list.1 = 10 list.1.1 = 11 list.1.2 = 12 say list.1 say list.1.1 say list.1.2 The output of the above program will be shown as follows − 10 11 12 The following point needs to be noted about the above program − To create a multidimensional array, we can use another layer of indexing. So in our example, we used list.1.1 to create another inner array for the index value 1 of the list array. Print Page Previous Next Advertisements ”;
Rexx – Home
Rexx Tutorial PDF Version Quick Guide Resources Job Search Discussion Rexx (Restructured Extended Executor) is designed to be a scripting language. Its goal is to make scripting as easy, fast, reliable, and error-free as possible. Many programming languages are designed for compatibility with older languages, and are written for specific audiences or platforms. Rexx ignores extraneous objectives. It was designed from day one to be powerful, yet easy to use. It is also very helpful for developing small programs that perform various text file transformations. This is an introductory tutorial that covers the basics of Rexx and how to deal with its various components and sub-components. Audience This tutorial has been prepared mainly for those professionals who are within the IT industry, working as specialists in the field of Scripting and Macro Languages. It is very useful for those professionals who work on Data Processing, text and also for generating reports. This tutorial is intended to make you comfortable in getting started with Restructured Extended Executor (Rexx) and its various functions. Prerequisites It is an elementary tutorial and you can easily understand the concepts explained here with a basic knowledge of how a company or an organization deals with its scripting languages and programs. However, it will help if you have some prior exposure on programming languages, data processing, and generating reports. Print Page Previous Next Advertisements ”;
Rexx – Loops
Rexx – Loops ”; Previous Next So far we have seen statements which have been executed one after the other in a sequential manner. Additionally, statements are provided in Rexx to alter the flow of control in a program’s logic. They are then classified into a flow of control statements which we will study in detail. A loop statement allows us to execute a statement or group of statements multiple times. The following illustration is the general form of a loop statement in most of the programming languages. Let us discuss various loops supported by Rexx. Sr.No. Loop Type & Description 1 do loop The do loop is used to execute a number of statements for a certain number of times. The number of times that the statement needs to be executed is determined by the value passed to the do loop. 2 do-while loop The do-while statement is used to simulate the simple while loop which is present in other programming languages. 3 do-until loop The do-until loop is a slight variation of the do while loop. This loop varies in the fact that is exits when the condition being evaluated is false. Controlled Repetition The do loops can be catered to carry out a controlled repetition of statements. Syntax The general syntax of this sort of statement is as follows. do index = start [to limit] [by increment] [for count] statement #1 statement #2 end The difference in this statement is that there is an index which is used to control the number of times the loop is executed. Secondly, there are parameters which state the value which the index should start with, where it should end and what is the increment value. Flow Diagram Let’s check out the flow diagram of this loop − From the above diagram you can clearly see that the loop is executed based on the index value and how the index value is incremented. The following program is an example of the controlled repetition statement. Example Live Demo /* Main program */ do i = 0 to 5 by 2 say “hello” end In the above program, the value of the count i is set to 0 first. Then it is incremented in counts of 2 till the value is not greater than 5. The output of the above code will be − hello hello hello Print Page Previous Next Advertisements ”;
Prolog – Built-In Predicates
Prolog – Built-In Predicates ”; Previous Next In Prolog, we have seen the user defined predicates in most of the cases, but there are some built-in-predicates. There are three types of built-in predicates as given below − Identifying terms Decomposing structures Collecting all solutions So this is the list of some predicates that are falls under the identifying terms group − Predicate Description var(X) succeeds if X is currently an un-instantiated variable. novar(X) succeeds if X is not a variable, or already instantiated atom(X) is true if X currently stands for an atom number(X) is true if X currently stands for a number integer(X) is true if X currently stands for an integer float(X) is true if X currently stands for a real number. atomic(X) is true if X currently stands for a number or an atom. compound(X) is true if X currently stands for a structure. ground(X) succeeds if X does not contain any un-instantiated variables. The var(X) Predicate When X is not initialized, then, it will show true, otherwise false. So let us see an example. Example | ?- var(X). yes | ?- X = 5, var(X). no | ?- var([X]). no | ?- The novar(X) Predicate When X is not initialized, the, it will show false, otherwise true. So let us see an example. Example | ?- nonvar(X). no | ?- X = 5,nonvar(X). X = 5 yes | ?- nonvar([X]). yes | ?- The atom(X) Predicate This will return true, when a non-variable term with 0 argument and a not numeric term is passed as X, otherwise false. Example | ?- atom(paul). yes | ?- X = paul,atom(X). X = paul yes | ?- atom([]). yes | ?- atom([a,b]). no | ?- The number(X) Predicate This will return true, X stands for any number, otherwise false. Example | ?- number(X). no | ?- X=5,number(X). X = 5 yes | ?- number(5.46). yes | ?- The integer(X) Predicate This will return true, when X is a positive or negative integer value, otherwise false. Example | ?- integer(5). yes | ?- integer(5.46). no | ?- The float(X) Predicate This will return true, X is a floating point number, otherwise false. Example | ?- float(5). no | ?- float(5.46). yes | ?- The atomic(X) Predicate We have atom(X), that is too specific, it returns false for numeric data, the atomic(X) is like atom(X) but it accepts number. Example | ?- atom(5). no | ?- atomic(5). yes | ?- The compound(X) Predicate If atomic(X) fails, then the terms are either one non-instantiated variable (that can be tested with var(X)) or a compound term. Compound will be true when we pass some compound structure. Example | ?- compound([]). no | ?- compound([a]). yes | ?- compound(b(a)). yes | ?- The ground(X) Predicate This will return true, if X does not contain any un-instantiated variables. This also checks inside the compound terms, otherwise returns false. Example | ?- ground(X). no | ?- ground(a(b,X)). no | ?- ground(a). yes | ?- ground([a,b,c]). yes | ?- Decomposing Structures Now we will see, another group of built-in predicates, that is Decomposing structures. We have seen the identifying terms before. So when we are using compound structures we cannot use a variable to check or make a functor. It will return error. So functor name cannot be represented by a variable. Error X = tree, Y = X(maple). Syntax error Y=X>(maple) Now, let us see some inbuilt predicates that falls under the Decomposing structures group. The functor(T,F,N) Predicate This returns true if F is the principal functor of T, and N is the arity of F. Note − Arity means the number of attributes. Example | ?- functor(t(f(X),a,T),Func,N). Func = t N = 3 (15 ms) yes | ?- The arg(N,Term,A) Predicate This returns true if A is the Nth argument in Term. Otherwise returns false. Example | ?- arg(1,t(t(X),[]),A). A = t(X) yes | ?- arg(2,t(t(X),[]),A). A = [] yes | ?- Now, let us see another example. In this example, we are checking that the first argument of D will be 12, the second argument will be apr and the third argument will be 2020. Example | ?- functor(D,date,3), arg(1,D,12), arg(2,D,apr), arg(3,D,2020). D = date(12,apr,2020) yes | ?- The ../2 Predicate This is another predicate represented as double dot (..). This takes 2 arguments, so ‘/2’ is written. So Term = .. L, this is true if L is a list that contains the functor of Term, followed by its arguments. Example | ?- f(a,b) =.. L. L = [f,a,b] yes | ?- T =.. [is_blue,sam,today]. T = is_blue(sam,today) yes | ?- By representing the component of a structure as a list, they can be recursively processed without knowing the functor name. Let us see another example − Example | ?- f(2,3)=..[F,N|Y], N1 is N*3, L=..[F,N1|Y]. F = f L = f(6,3) N = 2 N1 = 6 Y = [3] yes | ?- Collecting All Solutions Now let us see the third category called the collecting all solutions, that falls under built-in predicates in Prolog. We have seen that to generate all of the given solutions of a given goal using the semicolon in the prompt. So here is an example of it. Example | ?- member(X, [1,2,3,4]). X = 1 ? ; X = 2 ? ; X = 3 ? ; X = 4 yes Sometimes, we need to generate all of the solutions to some goal within a program in some AI related applications. So there are three built-in predicates that will help us to get the results. These predicates are as follows − findall/3 setoff/3 bagof/3 These three predicates take three arguments, so we have written ‘/3’ after the name of the predicates. These are also known as meta-predicates. These