Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Reduce (computer algebra system)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Syntax == The REDUCE language is a [[High-level programming language|high-level]] [[Structured programming|structured]] programming language based on [[ALGOL 60]] (but with Standard Lisp [[Semantics (computer science)|semantics]]), although it does not support all ALGOL 60 syntax. It is similar to [[Pascal (programming language)|Pascal]], which evolved from ALGOL 60, and [[Modula]], which evolved from Pascal. REDUCE is a [[free-form language]], meaning that spacing and line breaks are not significant, but consequently input statements must be separated from each other and all input must be terminated with either a semi-colon (<code>;</code>) or a dollar sign (<code>$</code>). The difference is that if the input results in a useful (non-<code>nil</code>) value then it will be output if the separator is a semi-colon (<code>;</code>) but hidden if it is a dollar sign (<code>$</code>). The assignment operator is colon-equal (<code>:=</code>), which in its simplest usage assigns to the variable on its left the value of the expression on its right. However, a REDUCE variable can have no value, in which case it is displayed as its name, in order to allow mathematical expressions involving indeterminates to be constructed and manipulated. The simplest way to use REDUCE is interactively: type input after the last input [[Command-line interface|prompt]], terminate it with semi-colon and press the ''[[Enter key|Return]]'' or ''[[Enter key|Enter]]'' key; REDUCE processes the input and displays the result. This is illustrated in the [[:File:REDUCE Screenshot 1.png|screenshot]]. === Identifiers and strings === Programming languages use [[Identifier (computer languages)|identifiers]] to name constructs such as variables and functions, and [[String (computer science)|strings]] to store text. A REDUCE identifier must begin with a letter and can be followed by letters, digits and underscore characters (<code>_</code>). A REDUCE identifier can also include any character anywhere if it is input preceded by an exclamation mark (<code>!</code>). A REDUCE string is any sequence of characters delimited when input by double quote characters (<code>"</code>). A double quote can be included in a string by entering two double quotes; no other escape mechanism is implemented within strings. An identifier can be used instead of a string in most situations in REDUCE, such as to represent a file name. REDUCE source code was originally written in all [[Letter case|upper-case]] letters, as were all programming languages in the 1960s. (Hence, the name REDUCE is normally written in all upper-case.) However, modern REDUCE is [[Case sensitivity|case-insensitive]] (by default), which means that it ignores the case of letters, and it is normally written in lower-case. (The REDUCE source code has been converted to lower case.) The exceptions to this rule are that case is preserved within strings and when letters in identifiers are preceded by an exclamation mark (<code>!</code>). Hence, it is conventional to use [[Snake case|snake-case]] (e.g. <code>long_name</code>) rather than [[Camel case|camel-case]] (e.g. <code>longName</code>) for REDUCE identifiers, because camel-case gets lost without also using exclamation marks. === Hello World programs === Below is a REDUCE [["Hello, World!" program]], which is almost as short as such a program could possibly be! <syntaxhighlight lang="octave" copy> "Hello, World!"; </syntaxhighlight> REDUCE displays the output <div class="center" style="width: auto; margin-left: auto; margin-right: auto;"> Hello, World! </div> Another REDUCE "Hello, World!" program, which is slightly longer than the version above, uses an identifier as follows <syntaxhighlight lang="octave" copy> !Hello!,! !World!!; </syntaxhighlight> CSL REDUCE displays the same output as shown above. (Other REDUCE GUIs may italicise this output on the grounds that it is an identifier rather than a string.) === Statements and expressions === Because REDUCE inherits Lisp semantics, all programming constructs have values. Therefore, the only distinction between statements and expressions is that the value of an expression is used but the value of a statement is not. The terms ''statement'' and ''expression'' are interchangeable, although a few constructs always return the Lisp value <code>nil</code> and so are always used as statements. There are two ways to group several statements or expressions into a single unit that is syntactically equivalent to a single statement or expression, which is necessary to facilitate [[structured programming]]. One is the <code>begin</code>...<code>end</code> construct inherited from ALGOL 60, which is called a ''block'' or ''compound'' statement. Its value is the value of the expression following the (optional) keyword <code>return</code>. The other uses the bracketing syntax <code><<</code>...<code>>></code>, which is called a ''group'' statement. Its value is the value of the last (unterminated) expression in it. Both are illustrated below in the [[#Procedural programming|procedural programming example]] below. === Structured programming === REDUCE supports [[Conditional (computer programming)|conditional]] and repetition statements, some of which are controlled by a ''boolean expression'', which is any expression whose value can be either ''true'' or ''false'', such as <math>x>0</math>. (The REDUCE user-level language does not explicitly support constants representing ''true'' or ''false'' although, as in [[C (programming language)|C]] and related languages, 0 has the boolean value ''false'', whereas 1 and many other non-zero values have the boolean value ''true''.) ==== Conditional statements: if ... then ... else ==== The conditional statement has the form <div class="center" style="width: auto; margin-left: auto; margin-right: auto;"> <code>if</code> ''boolean expression'' <code>then</code> ''statement'' </div> which can optionally be followed by <div class="center" style="width: auto; margin-left: auto; margin-right: auto;"> <code>else</code> ''statement'' </div> For example, the following conditional statement ensures that the value of <math>n</math>, assumed to be numerical, is positive. (It effectively implements the [[Absolute value (algebra)|absolute value]] function.) <syntaxhighlight lang="octave"> if n < 0 then n := -n </syntaxhighlight> The following conditional statement, used as an expression, avoids an error that would be caused by dividing by 0. <syntaxhighlight lang="octave"> recip_x := if x = 0 then infinity else 1/x </syntaxhighlight> ==== Repetition statements: for ... ==== The <code>for</code> statement is a flexible loop construct that executes ''statement'' repeatedly a number of times that must be known in advance. One version has the form <div class="center" style="width: auto; margin-left: auto; margin-right: auto;"> <code>for</code> ''variable'' := ''initial'' <code>step</code> ''increment'' <code>until</code> ''final'' <code>do</code> ''statement'' </div> where ''variable'' names a variable whose value can be used within ''statement'', and ''initial, increment'' and ''final'' are numbers (preferably integers). The value of ''variable'' is initialized to ''initial'' and ''statement'' is executed, then the value of ''variable'' is repeatedly increased by ''increment'' and the ''statement'' executed again, provided the value of ''variable'' is not greater than ''final''. The common special case "''initial'' <code>step 1 until</code> ''final''" can be abbreviated as "''initial'' : ''final''". The following <code>for</code> statement computes the value of <math>n!</math> as the value of the variable <code>fac</code>. <syntaxhighlight lang="octave" copy=""> n := 5; fac := 1$ for r := 2 : n do fac := fac*r; fac; </syntaxhighlight> Another version of the <code>for</code> statement iterates over a list, and the keyword <code>do</code> can be replaced by <code>product</code>, <code>sum</code>, <code>collect</code> or <code>join</code>, in which case the <code>for</code> statement becomes an expression and the controlled ''statement'' is treated as an expression. With <code>product</code>, the value is the product of the values of the controlled ''statement''; with <code>sum</code>, the value is the sum of the values of the controlled ''statement''; with <code>collect</code>, the value is the values of the controlled ''statement'' collected into a list; with <code>join</code>, the value is the values of the controlled ''statement'', which must be lists, joined into one list. The following <code>for</code> statement computes the value of <math>n!</math> much more succinctly and elegantly than the previous example. <syntaxhighlight lang="octave" copy=""> n := 5; for r := 2 : n product r; </syntaxhighlight> ==== Repetition statements: while ... do; repeat ... until ==== The two loop statements <div class="center" style="width: auto; margin-left: auto; margin-right: auto;"> <code>while</code> ''boolean expression'' <code>do</code> ''statement''<br /><code>repeat</code> ''statement'' <code>until</code> ''boolean expression'' </div> are closely related to the conditional statement and execute ''statement'' repeatedly a number of times that need not be known in advance. Their difference is that <code>while</code> repetition stops when ''boolean expression'' becomes false whereas <code>repeat</code> repetition stops when ''boolean expression'' becomes true. Also, <code>repeat</code> always executes ''statement'' at least once and it can be used to initialize ''boolean expression'', whereas when using <code>while</code> ''boolean expression'' must be initialized before entering the loop. The following <code>while</code> statement computes the value of <math>n!</math> as the value of the variable <code>fac</code>. Note that this code treats the assignment <code>n := n - 1</code> as an expression and uses its value. <syntaxhighlight lang="octave" copy> n := 5; fac := n$ while n > 1 do fac := fac*(n := n - 1); fac; </syntaxhighlight> === Comments === REDUCE has three comment conventions. It inherits the ''comment statement'' from ALGOL 60, which looks like this: comment This is a multi-line comment that ends at the next separator, so it cannot contain separators; Comment statements mostly appear in older code. It inherits the <code>%</code>... comment from Standard Lisp, which looks like this: % This is a single-line comment that ends at the end of the line. % It can appear on a line after code and % can contain the separators ";" and "$". <code>%</code>... comments are analogous to [[C++]] <code>//</code>... comments and are the most commonly used form of comment. REDUCE also supports a [[C (programming language)|C]]-style <code>/*</code>...<code>*/</code> comment that looks like this: /* This is a multi-line comment that can appear anywhere a space could and can contain the separators ";" and "$". */
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)