• Ei tuloksia

Language Elements

In document 1.1 Getting Started (sivua 125-134)

Table 4.1 Special Characters blank ( left parenthesis

; semicolon ) right parenthesis : colon [ left bracket

+ plus ] right bracket

- minus { left brace

* asterisk } right brace

/ slash ‘ back quote

^ caret ’ single quote (apostrophe)

! exclamation " double quote

= equal | vertical bar

< less than & ampersand

> greater than _ underscore

@ at sign % percent

$ dollar \ backslash

. period # sharp

, comma ? question mark

Tokens

Maple’s language definition combines characters into tokens. Tokens consist of keywords (reserved words), programming-language operators, names, strings, natural integers, and punctuation marks.

Reserved Words Table 4.2 lists thereserved wordsin Maple. They have special meanings, and thus you cannot use them as variables in programs.

Many other symbols in Maple have predefined meanings. For example, mathematical functions such as sin and cos, Maple commands such as expandand simplify, and type names such asinteger andlist. How-ever, you can safely use these commands in Maple programs in certain contexts. But the reserved words in table 4.2 have a special meanings, and thus you cannot change them.

Programming-Language Operators Three types of Maple language operators exist, namely binary, unary, and nullary operators. Tables 4.3 and 4.4 list these operators and their uses. The three nullary oper-ators, %,%%, and %%% are special Maple names which refer to the three previously computed expressions.

The ?precedence help page gives the order of precedence of all programming-language operators.

4.1 Language Elements 115

Table 4.2 Reserved Words

Keywords Purpose

break,next loop control

if,then,elif,else ifstatement

for,from,in,by,to, forand whileloops while,do

proc,local,global,option, procedures error,return options,description

export,module,use modules

end ends structure

assuming assume facility

try,catch,finally exception handling

read,save read andsave statements

quit,done,stop ending Maple

union,minus,intersect,subset set operators and,or,not,xor,implies Boolean operators

mod modulus operator

Table 4.3 Programming Binary Operators

Operator Meaning Operator Meaning

+ addition < less than

- subtraction <= less or equal

* multiplication > greater than

/ division >= greater or equal

^ exponentiation <> not equal

$ sequence operator -> arrow operator

@ composition union set union

@@ repeated composition minus set difference

&string neutral operator intersect set intersection , expression separator :: type declaration,

|| concatenation pattern binding

. decimal point and logical and

.. ellipsis or logical or

mod modulo . non-commutative

:= assignment multiplication

Table 4.4 Programming Unary Operators Operator Meaning

+ unary plus (prefix) - unary minus (prefix)

! factorial (postfix)

$ sequence operator (prefix) not logical not (prefix)

&string neutral operator (prefix)

. decimal point (prefix or postfix)

%integer label (prefix)

Names Maple’s language definition predefines many other tokens, in-cluding names. For example, mathematical functions likesinandcos, or commands likeexpandorsimplify, or type names likeinteger orlist are all examples of names.

The simplest instance of aname consists of letters, digits, and under-scores, and does not begin with a number. Maple reserves names beginning with an underscore for internal use only.

Names of the form~name are allowed for spreadsheet references.

Strings Maple’s language definition also predefines strings. Some simple strings are"h","hi","result"and"Input value1". Generally, enclos-ing any sequence of characters in double quotes forms a strenclos-ing.

> "The modulus should be prime";

“The modulus should be prime”

> "There were %d values";

“There were %d values”

You should not confuse the double quote character,", which delimits a string, with the back quote character,‘, which forms a symbol or the single quote,’, which delays evaluation. A string’s length has no practical limit in Maple. On most Maple implementations, this means that a string can contain more than half a million characters.

To make the double quote character appear in a string, type a back-slash character and a double quote (") where you want the double quote character to appear.

4.1 Language Elements 117

> "a\"b";

“a\“b”

Similarly, to allow a backslash (escape character) to appear as one of the characters in a string, type two consecutive backslashes\\.

> "a\\b";

“a\\b”

The special backslash characters mentioned above only count as one character, as is demonstrated by using thelengthcommand.

> length(%);

3

A reserved word enclosed in double quotes also becomes a valid Maple string, distinct from its usage as a token.

> "while";

“while”

The enclosing double quotes themselves do not form part of the string.

> length("abcde");

5

To access individual characters or substrings, strings can be sub-scripted in much the same way as lists. An integer range provides access.

> S := "This is a string";

S:= “This is a string”

> S[6..9];

“is a”

> S[-6..-1];

“string”

As well, iterations can be performed over the characters in a string.

> seq(i,i="over a string");

“o”,“v”,“e”,“r”,“ ”,“a”,“ ”,“s”,“t”,“r”,“i”,“n”,“g”

Integers Anatural integer is any sequence of one or more digits. Maple ignores any leading zeroes.

> 03141592653589793238462643;

3141592653589793238462643

The length limit for integers is system-dependent, but is generally much larger than users require.

An integer is either a natural integer or a signed integer. Either +natural or-natural indicates a signed integer.

> -12345678901234567890;

−12345678901234567890

> +12345678901234567890;

12345678901234567890

Token Separators

You can separate tokens by using either white space or punctuation marks. This tells Maple where one token ends and the next begins.

Blanks, Lines, Comments, and Continuation Thewhite space charac-ters are space, tab, return, and line-feed. This book uses the terminology newline to refer to either return or line-feed since the Maple system does not distinguish between these characters. The terminology blank refers to either space or tab. The white space characters separate tokens, but are not themselves tokens.

White space characters cannot normally occur within a token.

4.1 Language Elements 119

> a: = b;

syntax error, ‘=‘ unexpected:

a: = b;

^

You can use white space characters freely between tokens.

> a * x + x*y;

a x+x y

The only instances in which white space can become part of a token are names and strings, formed by enclosing a sequence of characters in back quotes and double quotes, respectively. In these cases, the white space characters are as significant as any other character.

On a line, unless you are in the middle of a string, Maple considers all characters which follow a sharp character “#” to be part of a comment.

Since white space and newline characters are functionally the same, you can continue statements from line to line.

> a:= 1 + x +

> x^2;

a:= 1 +x+x2

The problem of continuation from one line to the next is less trivial when long numbers or long strings are involved since these two classes of tokens are not restricted to a few characters in length. The general mech-anism in Maple to specify continuation of one line onto the next is as follows: if the special character backslash,\, immediately precedes a new-line character, then the parser ignores both the backslash and the newnew-line.

If a backslash occurs in the middle of a line, Maple usually ignores it; see

?backslashfor exceptions. You can use this to break up a long sequence of digits into groups of smaller sequences, to enhance readability.

> "The input should be either a list of\

> variables or a set of variables";

“The input should be either a list of variables or \ a set of variables”

Table 4.5 Maple Punctuation Marks

; semicolon ( left parenthesis

: colon ) right parenthesis

’ single quote [ left bracket

‘ back quote ] right bracket

| vertical bar { left brace

< left angle bracket } right brace

> right angle bracket , comma

> G:= 0.5772156649\0153286060\

> 6512090082\4024310421\5933593992;

G:=.5772156649015328606065120900824024\

3104215933593992

Punctuation Marks Table 4.5 lists thepunctuation marks.

; and : Use the semicolon and the colon to separate statements. The distinction between these marks is that a colon during an interactive session prevents the result of the statement from printing.

> f:=x->x^2;

f :=x→x2

> p:=plot(f(x), x=0..10):

’ Enclosing an expression, or part of an expression, in a pair of single quotes delays evaluation of the expression (subexpression) by one level. See section 4.4.

> ’’sin’’(Pi);

’sin’(π)

> %;

sin(π)

4.1 Language Elements 121

> %;

0

‘ To form symbols, use the back quote character.

> limit(f(x), x=0, ‘right‘):

() The left and right parentheses group terms in an expression and group parameters in a function call.

> (a+b)*c; cos(Pi):

[] Use the left and right square brackets to form indexed (subscripted) names and to select components from aggregate objects such as arrays, rtables, tables, and lists. See section 4.4.

> a[1]: L:=[2,3,5,7]: L[3]:

[] and {} Use the left and right square brackets also to form lists, and the left and right braces to form sets. See section 4.4.

> L:=[2,3,5,2]: S:={2,3,5,2}:

<> and | The left and right angle brackets in conjunction with the ver-tical bar are used to construct rtable-based Matrices and Vectors.

> <<1,2,3> | <4,5,6>>:

, Use the comma to form a sequence, to separate the arguments of a func-tion call, and to separate the elements of a list or set. See secfunc-tion 4.4.

> sin(Pi), 0, limit(cos(xi)/xi, xi=infinity):

In document 1.1 Getting Started (sivua 125-134)