Contents Previous Next Index
|
2 Maple Language Elements
|
|
Before programming in Maple, it is important to learn the properties and roles of the basic elements of the Maple language. This chapter introduces some of the main concepts, which will be described in more detail later in this guide.
|
2.1 In This Chapter
|
|
•
|
Basic elements of the Maple language: the character set and tokens
|
•
|
Maple tokens: reserved words, operators, names, strings, and natural numbers; function types
|
•
|
Using special characters
|
•
|
Maple data types related to the tokens
|
|
|
2.2 Character Set
|
|
The Maple character set consists of letters, digits, and special characters. These include 26 lowercase letters, 26 uppercase letters, and 10 decimal digits.
a b c d e f g h i j k l m n o p q r s t u v w x y z
|
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
|
|
|
There are also 33 special characters, which are listed in Table 2.1. These characters, or combinations of these characters, have special meanings in the Maple language.
Table 2.1: Special Characters |
Character
|
Meaning
|
Character
|
Meaning
|
|
blank
|
(
|
left parenthesis
|
;
|
semicolon
|
)
|
right parenthesis
|
:
|
colon
|
[
|
left bracket
|
+
|
plus
|
]
|
right bracket
|
-
|
minus
|
{
|
left brace
|
*
|
asterisk
|
}
|
right brace
|
/
|
slash
|
`
|
left single quote (back quote)
|
^
|
caret
|
'
|
right single quote (apostrophe)
|
!
|
exclamation
|
"
|
double quote
|
=
|
equal
|
|
|
vertical bar
|
<
|
less than
|
&
|
ampersand
|
>
|
greater than
|
_
|
underscore
|
@
|
at sign
|
%
|
percent
|
$
|
dollar
|
\
|
backslash
|
.
|
period
|
#
|
pound sign (sharp)
|
,
|
comma
|
?
|
question mark
|
~
|
tilde
|
|
|
|
These are the only characters used in the Maple language. However, all character types can be used in names and strings, including international characters. For more information on how to create names using international characters, see Names.
Note: When you manipulate a string or determine the length of a string, non-ASCII and international characters may be counted as more than one byte.
Many string manipulation commands interpret multibyte characters as multiple characters.
| (1) |
| (2) |
|
|
2.3 Tokens
|
|
The Maple language combines characters into tokens. The set of tokens consists of reserved words (also called keywords), programming-language operators, names, strings, and natural integers.
|
Reserved Words
|
|
Maple keywords are reserved words that have special meanings. Therefore, you cannot change them or use them as variables in procedures. The keywords are listed in Table 2.2. You can find information about specific keywords in later chapters of this guide or the help system.
For more information about reserved words in Maple, refer to the keyword help page.
Table 2.2: Reserved Keywords |
Keywords
|
Purpose
|
break, next
|
loop control
|
if, then, elif, else
|
if statement
|
for, from, in, by, to, while, do
|
for and while loops
|
proc, local, global, option, error, return, options, description
|
procedures
|
export, module, use
|
modules
|
end
|
ends structures
|
assuming
|
assume facility
|
try, catch, finally
|
exception handling
|
read, save
|
read and save statements
|
quit, done, stop
|
ending Maple
|
union, minus, intersect, subset
|
set operators
|
and, or, not, xor
|
Boolean operators
|
implies
|
implication operator
|
mod
|
modulus operator
|
|
|
|
Programming-Language Operators
|
|
There are two main types of Maple language operators: unary and binary. Simply put, a unary operator acts on one operand, as in -a, where the operator - is applied to a. A binary operator acts on two operands, as in a+b, where + is the operator and the operands are a and b.
The Maple binary and unary operators, and their meanings, are listed in Table 2.3 and Table 2.4. For more information about these operators, refer to the operators,binary and operators,unary help topics.
For information about the order of precedence of programming-language operators, refer to the operators/precedence help page.
Table 2.3: 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
|
=
|
equal or equation
|
@
|
composition
|
union
|
set union
|
@@
|
repeated composition
|
minus
|
set difference
|
&string
|
neutral operator
|
intersect
|
set intersection
|
,
|
expression separator
|
::
|
type declaration and pattern binding
|
||
|
concatenation
|
in
|
membership
|
.
|
non-commutative multiplication
|
and
|
logical and
|
->
|
arrow operator
|
or
|
logical or
|
..
|
ellipsis
|
xor
|
exclusive or
|
mod
|
modulo
|
implies
|
implication
|
:=
|
assignment
|
subset
|
subset
|
|
Table 2.4: 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)
|
|
Most of the unary and binary operators can also be used in element-wise form with objects that have multiple elements. To perform an element-wise operation, add a trailing tilde (~) after an operator that has an element-wise form. An element-wise operation allows you to apply an operation to the elements of a list, set, table, Array, Matrix, or Vector. For example, compare Matrix multiplication with element-wise multiplication of paired entries in a Matrix.
| (3) |
>
|
<1,2;3,4> .~ <2,2;2,2>;
|
| (4) |
The Maple element-wise operators are listed in Table 2.5. For more information about these operators, refer to the operators,elementwise help page.
Table 2.5: Element-wise Operators |
Element-wise Operator
|
Meaning
|
Element-wise Operator
|
Meaning
|
+~
|
addition or unary plus
|
<~
|
less than
|
-~
|
subtraction or unary minus
|
<=~
|
less or equal
|
*~
|
multiplication
|
>~
|
greater than
|
/~
|
division
|
>=~
|
greater or equal
|
^~
|
exponentiation
|
<>~
|
not equal
|
!~
|
factorial (unary postfix)
|
=~
|
equal or equation
|
@~
|
composition
|
union~
|
set union
|
@@~
|
repeated composition
|
minus~
|
set difference
|
&name ~
|
neutral operator
|
intersect~
|
set intersection
|
&name ~
|
neutral operator (unary prefix)
|
in~
|
membership
|
subset~
|
subset
|
or~
|
logical or
|
.~
|
non-commutative multiplication
|
and~
|
logical and
|
||~
|
concatenation
|
xor~
|
exclusive or
|
mod~
|
modulo
|
implies~
|
implication
|
funct~
|
element-wise
|
not~
|
logical not (unary prefix)
|
|
Also, three special nullary operators (also called ditto operators) can be used in interactive sessions. These are special Maple names that can be used to refer to previously computed non-NULL expressions.
% last expression
|
%% second-last expression
|
%%% third-last expression
|
|
|
While they can be used for simple computations, the ditto operators should be avoided when writing programs. For results that need to be used in subsequent expressions, assign values to variables instead.
Note: In a worksheet, the ditto operators do not necessarily reference the results of the lines located above the execution groups in which they are used. They reference the results of the most recently performed computations in the Maple session, regardless of the execution group or document in which they are located. Also, in terms of evaluation, the ditto operators are treated differently than local variables in a procedure. They are fully evaluated, which may require more processing than one-level evaluation of local variables. For more information about local variables, see Local Variables.
For more information about the ditto operators, refer to the ditto help page.
|
|
Names
|
|
A name in Maple is a sequence of one or more characters that uniquely identifies a command, file, variable, or other entity.
The simplest instance of a name consists of a letter followed by a sequence of letters, digits, and underscores.
| (5) |
If you need to create a name that includes blank spaces or international characters, use left single quotes (`).
| (6) |
| (7) |
In general any name that can be formed without left single quotes is identical to the same name with quotes. For example, x and `x` refer to the same name x. Left single quotes are similar to double quotes in that double quotes are used to build strings while left single quotes are used to build names.
Note that the reverse is not true, some names can be formed with left single quotes that are not identical to expressions typed in without quotes. One example is the name `2`. By putting quotes around the 2 here, a name is formed instead of a number. Another example is a quoted keyword, like `module`. To test if an expression is of type module, check type(expr,`module`). Without the quotes, the Maple parser determines that this is the start of a module definition and the parser will flag a syntax error.
Characters in Maple are case-sensitive. Therefore, for example, the name Apple is different from the name apple.
| (8) |
| (9) |
| (10) |
Other Maple names are used for
•
|
mathematical functions such as sin and cos
|
•
|
Maple commands such as expand or simplify
|
•
|
type names such as integer or list
|
•
|
symbols, for example, x and y in the expression x+y
|
•
|
variables, or names with assigned values
|
For example, in the first statement below, y is a name that does not have a value. In the second statement, the variable x has the value 3.
| (11) |
| (12) |
You can create an empty name, which has no characters in its spelling.
| (13) |
Early versions of Maple did not have separate types for names and strings. As a result, many commands for string processing will also accept names and process their characters the same way. It is generally better to use strings for such processing as strings can never have assigned values.
For more more information about names, see Names.
|
|
|
2.4 Natural Integers
|
|
A natural integer is a sequence of one or more decimal digits.
>
|
00003141592653589793238462643;
|
| (14) |
For more information about integers, see Integers and Numeric Types in Maple.
|
|
2.5 Strings
|
|
A string is a sequence of characters that evaluates to itself. To create a string, enclose any sequence of characters in double quotes.
| (15) |
You cannot assign a value to a string.
In the following sections, strings and string operations are described. For information on the StringTools package, refer to the StringTools help page.
|
Length of a String
|
|
Use the length command to determine the number of bytes in a string.
>
|
length("What is the length of this string?");
|
| (16) |
All of the characters between, but excluding, the double quotes are counted. Each blank space is counted as one character. Non-ASCII characters may be counted as more than one byte.
The maximum string length is system-dependent and ranges from about 268 million bytes on 32-bit systems to more than 34 billion bytes on 64-bit systems.
|
|
Substrings
|
|
You can extract a substring of a string by using a subscripted integer range (also called a selection operation).
>
|
S := "This is a string";
|
| (17) |
| (18) |
| (19) |
Negative numbers in the range count backwards from the end of the string. -2 is the second last character in the string. Either range endpoint can also be left off to indicate from the beginning, or to the end.
| (20) |
| (21) |
|
|
Searching a String
|
|
To perform case-sensitive and case-insensitive string searching, use the SearchText and searchtext commands, respectively.
SearchText( pattern, exprString, range );
|
searchtext( pattern, exprString, range );
|
|
|
The SearchText command searches for exact matches of pattern in exprString. The searchtext command performs the same search, but it is case-insensitive. If pattern is found, Maple returns an integer indicating the position of the first character in pattern in exprString. If the pattern is not found in exprString, 0 is returned.
>
|
SearchText("my s", "This is my string.");
|
| (22) |
>
|
searchtext("My S", "This is my string.");
|
| (23) |
The optional range restricts the search to the specified range. It is equivalent to performing a search on a substring, and it is useful when the pattern occurs more than once in the string.
>
|
SearchText("is", "This is my string.", 4..-1);
|
| (24) |
|
|
String Concatenation
|
|
Strings can be formed through concatenation by using the cat command.
Here, the sequence parameter can contain any number of expressions that are separated by commas.
The cat command is commonly used to concatenate strings with names and integers, and the result returned has the type (name or string) of the first argument to cat.
| (25) |
>
|
cat( "The value of i is ", i, "." );
|
| (26) |
>
|
filename := cat( kernelopts(mapledir), kernelopts(dirsep), "lib" );
|
| (27) |
|
|
Mutability of Strings
|
|
Strings are not mutable objects in Maple. This means that appending text to a string is not done in-place, but involves allocating new storage for the result and copying the original text, plus the appended text, into that new space. This is typically not an issue unless you are incrementally processing large amounts of text. In the latter case, the StringBuffer command may be useful.
| (28) |
>
|
s:-append("The quick brown fox"):
|
>
|
s:-append("jumped over the lazy dog"):
|
| (29) |
For more information, refer to the StringBuffer help page.
|
|
Special Characters in Strings
|
|
To display the double quote character in a string, enter a backslash character (\) followed by a double quote (") where you want the double quote character to appear. For more information, refer to the backslash help page.
| (30) |
Similarly, to display a backslash character as one of the characters in a string, enter two consecutive backslash characters, \\. You must escape the backslash in this manner because backslash is itself a special character. For more information, see Blank Spaces, New Lines, Comments, and Continuation.
| (31) |
The special backslash character mentioned above counts as only one character, as demonstrated by using the length command.
| (32) |
Doubling up backslashes is most notable when entering full path names in Maple. For this situation it is easier to use forward slash instead. Forward slash is recognized as a directory separator on all platforms including Windows.
|
|
Parsing Strings
|
|
The parse command accepts any Maple string and parses the string as if it had been entered or read from a file. This is especially useful when you want to interpret commands typed into text-components inside your Maple document.
parse( exprString, option );
|
|
|
Without specifying extra options, the string should contain exactly one Maple expression. The expression is parsed and returned unevaluated.
| (33) |
| (34) |
If the string is syntactically incorrect, the parse command displays an error message of the form incorrect syntax in parse: ... (number).
The number indicates the offset in characters, counted from the beginning of the string, at which the syntax error was detected.
If the option statement is specified, the string is parsed and evaluated, and then the result is returned.
| (35) |
>
|
parse("sin(Pi)", 'statement');
|
| (36) |
Partial statements or incomplete expressions cannot be parsed. Multiple statements or expressions can be interpreted via multiple calls to parse using the lastread and offset options.
For more information, refer to the parse help page.
|
|
Converting Expressions to Strings
|
|
To convert an expression to a string, use the convert command.
Maple can convert a variety of expressions. For more information about expressions, see Maple Expressions. For more information about conversions in Maple, refer to the convert help page.
| (37) |
>
|
convert(a+b-c*d/e, 'string');
|
| (38) |
| (39) |
|
|
|
2.6 Using Special Characters
|
|
|
Token Separators
|
|
You can separate tokens by using white space characters or punctuation marks. The separator indicates the end of one token and the beginning of the next.
|
|
Blank Spaces, New Lines, Comments, and Continuation
|
|
The white space characters are space, tab, return, and line-feed. This guide uses the term new line to refer to a return or line-feed since the Maple system does not distinguish between these characters. The term blank refers to a space or tab.
The white space characters separate tokens, but are not themselves tokens. White space characters cannot normally be used within a token.
However, you can use white space characters between tokens.
| (40) |
White space characters can be part of a token in a name or string formed by enclosing a sequence of characters in left single quotes or double quotes respectively. For more information, see White Space Characters within a Token.
Except in a string, all characters that follow a pound sign "#" on a line are part of a comment.
For information about adding comments in Maple procedures, see Adding Comments to a Procedure.
>
|
a := 1 + x + x^2; #This is a comment
|
| (41) |
Since white space and new line characters are functionally identical, you can continue statements from line to line, as described in Entering a Procedure Definition.
| (42) |
Note: Press Shift+Enter to continue typing on the next line without evaluating the expression.
To enter a long number or string on multiple lines, use the backslash character (\) as a line continuation character.
Line continuation functions as follows: if a backslash \ immediately precedes a new line character, the Maple parser ignores both the backslash and the new line. If a backslash is in the middle of a line, Maple usually ignores it. For more information about the backslash character and exceptions to this rule, refer to the backslash help page.
You can use the backslash character 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";
|
| (43) |
>
|
G:= 0.57721566490153286060\
6512090082402\43104215933593992;
|
| (44) |
You can also enter long strings by using a continuation character. Maple automatically concatenates string constants that are on separate lines, so another way to enter a long string is to close one set of double quotes at the end of a line and enter a new double quote at the beginning of the next line.
>
|
S:= "This is the start of a long string "
"and this is part of the same string with no line in between";
|
| (45) |
|
|
Punctuation Marks
|
|
The punctuation marks that act as token separators are listed in Table 2.6.
Table 2.6: Token Separators |
;
|
semicolon
|
(
|
left parenthesis
|
:
|
colon
|
)
|
right parenthesis
|
`
|
left single quote
|
[
|
left bracket
|
'
|
right single quote
|
]
|
right bracket
|
|
|
vertical bar
|
{
|
left brace
|
<
|
left angle bracket
|
}
|
right brace
|
>
|
right angle bracket
|
,
|
comma
|
|
|
Semicolon (;) and Colon (:)
|
|
Use the semicolon and the colon to separate statements. During an interactive session, a semicolon displays the result of the statement while a colon prevents the result of the statement from displaying.
| (46) |
>
|
p:=plot(f(x), x=0..10):
|
|
|
Right Single Quotes (')
|
|
Enclosing an expression, or part of an expression, in right single quotes (or apostrophes) delays the evaluation of an expression (subexpression) by one level. This is often used to ensure that procedure options are passed correctly as unevaluated names even when they have a value. For more information, see Unevaluated Expressions.
| (47) |
| (48) |
>
|
limit(1/x, x=0, 'right');
|
| (49) |
|
|
Left Single Quotes (`)
|
|
To form a name, enclose an expression in left single quotes.
| (50) |
|
|
Parentheses
|
|
The left and right parentheses group terms in an expression, arguments in a function call, and parameters in a procedure definition.
>
|
(a+b)*c; cos(Pi);
proc( x, y, z )
x+y+z;
end proc:
|
| (51) |
The left and right parentheses are also used to select components from certain data structures (programmer indexing).
|
|
Square Brackets
|
|
Use the left and right square brackets to form indexed (subscripted) names and to select components from data structures such as Arrays, tables, and lists. For more information on selection, see Indexed Expressions. For more information on mathematical indexing and programmer indexing, see Basic Data Access.
>
|
a[1]; L:=[2,3,5,7]; L[3];
|
| (52) |
|
|
Square Brackets and Braces
|
|
Use the left and right square brackets ([]) to form lists, and the left and right braces ({}) to form sets. For more information on sets and lists, see Immutable Data Structures.
>
|
L:=[2,3,5,2]; S:={2,3,5,2};
|
| (53) |
|
|
Angle Brackets
|
|
The left and right angle brackets (<>) in conjunction with the the comma, semicolon, and/or vertical bar (|) can be used to create Matrices and Vectors. For more information, refer to the Matrix and MVshortcut help pages.
| (54) |
| (55) |
|
|
Comma
|
|
Use the comma to form an expression sequence. Expression sequences are used to specify the arguments of a function call or the elements of a list or set.
>
|
sin(Pi), 0, limit(cos(xi)/xi, xi=infinity);
|
| (56) |
|
|
|
Escape Characters
|
|
An escape character indicates that the character that follows the escape character must be handled in a special manner. The escape characters in Maple are ?, !, #, and \.
? - The question mark character, if it appears as the first non-blank character on a line, opens the Maple help system. The words following ? on the same line determine the terms used to display a help page. Use either "," or "/" to separate the words that follow the question mark character. For more information, refer to the help help page.
! - The exclamation mark character, if it appears as the first non-blank character on a line, passes the remainder of the line as a command to the host operating system. For more information, refer to the system and escape help pages.
# and (*, *) - The pound sign character indicates that the characters that follow it on the line are a comment. The multiline comment characters, (*, and *) indicate the beginning and end of a comment. For more information, see Adding Comments to a Procedure or refer to the comment help page.
\ - The backslash character is used to continue lines, to group characters in a token, and introduce control characters. For more information, refer to the backslash help page.
|
|
|
2.7 Types and Operands
|
|
In most programming languages, data is divided into different classes of information called data types. In Maple, there is a logical or mathematical notion of type that is related to, but distinct from, the underlying data structure.
|
DAGs
|
|
All data in Maple is stored as a directed acyclic graph (DAG). An identifying tag for each DAG indicates what type of data it stores and how it is stored. Names, strings, lists, and positive and negative integers are examples of some DAG types. For a list of DAG types and how they are stored in memory, see Internal Representation. The op command (short for operand) can often be used to determine the DAG type of the underlying data by returning the zeroth operand. This only applies to certain data structures where op(0,e), where e is an expression, is defined as a special case. For more information, see the particular data structure help page.
| (57) |
| (58) |
| (59) |
| (60) |
However, the correspondence is not exact. A notable exception to this is function calls where the zeroth operand is the function name.
| (61) |
| (62) |
When names are assigned to data, they act as pointers in other languages. However, for most purposes, the Maple evaluation rules are such that you can think of them as variables. If you want to manipulate an assigned name rather than the data assigned to it, you can use right single quotes (also called unevaluation quotes), which are described in more detail in Unevaluated Expressions.
Assigned names that are pointers do not require type declarations as in low-level programming languages. This means that a name a may be assigned an integer and then later assigned a list without discretion. This system of weak typing means that, when writing robust code, you must verify types since variables may be assigned any value.
|
|
Maple Types
|
|
The type facility in Maple is accessed by using the type command. It is a mathematical type facility; however, some basic types such as integer or list map directly to the type of DAG. Some types, such as numeric, encapsulate a group of many different kinds of structures and structured types, such as name^integer, can match a very specific value.
Type checking is important in Maple to decide whether an expression is valid input for procedure calls and Maple commands. You can use the the type command or the :: operator for type checking. The operator form is primarily used to declare the type of a procedure parameter (see Procedures). The type command has the following syntax.
type( expression, typeName );
|
|
|
If the expression is of type typeName, the type command returns a value of true. Otherwise, a value of false is returned.
| (63) |
>
|
type("string", 'list');
|
| (64) |
>
|
type(123456, 'integer');
|
| (65) |
>
|
type(f(x), 'function');
|
| (66) |
The type of any integer is integer. The type command can also interpret many subtypes of integers, some of which are listed in Table 2.7.
Table 2.7: Subtype |
Subtype
|
Meaning
|
integer[8]
|
64-bit sized integer
|
integer[4]
|
32-bit sized integer
|
negint
|
negative integer
|
posint
|
positive integer
|
nonnegint
|
non-negative integer
|
nonposint
|
non-positive integer
|
even
|
even integer
|
odd
|
odd integer
|
prime
|
prime number
|
|
For more information, refer to the type help page, which also contains a complete list of types in Maple.
The type facility can also interpret compound or structured types such as list(integer) for a list of integers or list({negint,prime}) for a list of negative or prime integers.
>
|
type([-1, 2, 11], 'list({negint,prime})');
|
| (67) |
>
|
type([0, 2, 11], 'list({negint,prime})');
|
| (68) |
For more information about structured types, see type,structure.
|
|
Operands and op
|
|
In addition to providing information about the underlying type, the op command can provide information about the other operands or parts of a data structure. Most data structures in Maple can be divided into components. For example, an equation of the form x=y+x can be divided as follows.
•
|
the right-hand side, y+x
|
To determine the operands and the number of operands in an expression, use the op and nops commands respectively. These commands have the following basic syntax.
op( i, expression );
|
nops( expression );
|
|
|
If the optional first argument i to the op command is a positive integer, the ith operand of expression is returned.
| (69) |
| (70) |
| (71) |
| (72) |
>
|
op(0, eq)(op(1,eq), op(2,eq));
|
| (73) |
The op command can also determine the operands of an expression at various levels of a structure with the following syntax.
op( [i1, i2, ...], expression );
|
|
|
This syntax is equivalent to and more efficient than a nested call to the op command.
| (74) |
| (75) |
This hierarchical structure of expressions explains the name DAG. The internal representation of looks like an infix expression tree. See Figure 2.1
|
Figure 2.1: Expression Tree
|
|
|
For efficiency, Maple does not store multiple copies of identical objects, so the two x nodes in the tree can be represented in a picture like the one in Figure 2.2. In Figure 2.1 you see two "x" nodes in the tree, implying a copy of each "x". Figure 2.2 shows that the same instance of "x" is referred to in both places.
|
Figure 2.2: Expression DAG
|
|
|
The term directed acyclic graph simply refers to this variation of a tree where nodes may have multiple parents.
The tree form of an expression can be displayed using the dismantle command.
EQUATION(3)
NAME(4): x
SUM(5)
NAME(4): y
INTPOS(2): 1
NAME(4): x
INTPOS(2): 1
| |
This model is not exactly what is used in practice but the principle of uniqueness, with respect to nodes, still applies. Maple uses a more sophisticated internal representation for sums as described in Internal Representation. The real structure of the DAG shown in Figure 2.3.
|
Figure 2.3: Actual Expression DAG
|
|
|
The next three chapters introduce many of the other types in Maple and describe how to create and use them in programs.
|
|
|
2.8 Avoiding Common Problems
|
|
This section provides you with a list of common mistakes, examples, and hints that will help you understand and avoid common errors. Use this section to study the errors that you may encounter when entering the examples from this chapter in a Maple session.
|
Attempting to Assign to a Protected Name
|
|
An exception is raised if you attempt to assign a value to a protected name. To resolve this error, specify a different name.
For more information about protected names, see Protected Names or refer to the protect help page.
|
|
Invalid Left-Hand Assignment
|
|
An exception is raised if you attempt to assign a value to a string.
For more information about strings, see Strings or refer to the string help page.
Use only valid names on the left-hand side of an assignment statement.
|
|
Incorrect Syntax in Parse
|
|
The parse command accepts a string as its argument. An exception is raised if the string is syntactically incorrect.
For more information, refer to the parse help page.
The error message indicates the character number (counted from the left double quote) where the error was detected. In this case, the 6th character (the second minus sign) caused the error.
|
|
White Space Characters within a Token
|
|
An exception is normally raised if a white space character occurs in a token.
The less-than-or-equal operator <= is a token in Maple. Therefore, it cannot contain a space.
| (76) |
|
|
Incorrect Use of Double and Single Quotes
|
|
In Maple, double quotes form a string, left single quotes form a name, and right single quotes delay evaluation of an expression. Confusing a string with a name, or a name with delayed evaluation causes errors. Study the following examples to see the different uses of these quotes.
For more information about using quotes, see Punctuation Marks or refer to the quotes help page.
To form a string, enclose the expression in double quotes.
| (77) |
| (78) |
To form a name, enclose the expression in left single quotes. Unlike a string, which is displayed with double quotes around it, names are usually printed without quotes. The name in this example only looks like an expression.
| (79) |
| (80) |
To delay the evaluation of an expression, enclose it in right single quotes.
>
|
x := 2: y := 3: f := 'x + y';
|
| (81) |
| (82) |
|
|
Avoid Using Maple Keywords as Names
|
|
If you use a Maple keyword in a name, and do not enclose it in left single quotes, an exception is raised.
To resolve this issue, select a variable name that is not a Maple keyword.
Note: It is possible to use a Maple keyword as a name by enclosing it in left single quotes. For example,
| (83) |
| (84) |
However, this approach is not recommended, since it makes it very likely that errors will be introduced if, for example, you forget to place back-ticks around keywords. When possible, avoid using keywords as names.
|
|
|
2.9 Exercises
|
|
1.
|
Using Maple operators, do the following:
|
a
|
Compute the sum of 5434 and 6342.
|
b
|
Compute the product of 92 and 310.
|
c
|
Compute the quotient of the result from a) divided by the result from b).
|
d
|
Create a list containing the numbers from a), b), and c).
|
e
|
Square each element of the list from d).
|
1.
|
Create variables named "my quotient" and "my remainder". Use these variables and the irem command to find the integer quotient and remainder of 12345 divided by 234. Tip: Because the irem command stores extra results by assigning to the third argument you will need to make sure the name is passed and not its assigned value. Do this by using uneval quotes (').
|
2.
|
Compute 3^(3^98) modulo 7.
|
3.
|
Concatenate the three strings "int", "(x^2,", and "x)". Parse the resulting string. Evaluate the parsed string.
|
4.
|
Determine a random integer between 40 and 100 using the command rand(40..100). Concatenate this number with the string, "The student's grade is ". Extract the student's grade from the resulting string.
|
5.
|
Assign the expressions x^2 and x*x to the names a and b. Find the three operands of a and b. Compare the results with those returned by using the dismantle command, that is, dismantle(a) and dismantle(b). The dismantle command displays the internal data structure used.
|
|
|
Contents Previous Next Index
|