Application Center - Maplesoft

App Preview:

Maple Programming: 4.14: Working with execution groups and procedures

You can switch back to the summary page by clicking here.

Learn about Maple
Download Application


 

4.14.mws

Programming in Maple

Roger Kraft
Department of Mathematics, Computer Science, and Statistics
Purdue University Calumet

roger@calumet.purdue.edu

4.14. Working with execution groups and procedures

In this section we go over some practical tips on how to work with execution groups and procedure definitions. In particular, we will go over how to create and edit an execution group and how to create and edit a procedure definition.

An execution group is a collection of consecutive Maple input lines that are grouped together so that all of the Maple commands on the input lines are executed together when the cursor is placed anywhere within the lines and the Enter key is hit. The easiest way to create an execution group is to first use the key combination Ctrl-j to create however many input lines that are needed. Then place the cursor on the first of the input lines and use the F4 key (or the "Edit -> Split or Join -> Join Execution Groups" menu item) to combine the input lines into an execution group. Each time you hit the F4 key, the prompt below the current execution group is merged into the current execution group. Try this with the prompt at the end of this paragraph. Place the cursor at the prompt, type Ctrl-j three or four times, and then combine the separate input lines into a single execution group. (You can place some Maple commands after the prompts either before or after you combine them into an execution group.)

>   

After you create an execution group you will quite often need to edit it. If you just need to edit one of the lines in an execution group, there is not much of a problem. The non obvious part is when you want to enter a new line somewhere into the middle of an execution group. For example, suppose we want to enter a new line after the second line in the following execution group.

>    x^2-1;

>    factor( % );

>    expand( % );

x^2-1

(x-1)*(x+1)

x^2-1

There are several ways to do this. One way is to place the cursor at the beginning of the third line (just before expand ) and hit the F3 key (or use the "Edit -> Split or Join -> Split Execution Group" menu item). This will split off the third line from the two above it. Then either place the cursor in the third line and type Ctrl-k, or place the cursor in the first or second line and type Ctrl-j. Either way, you get a new input line between the factor  and expand  commands (and remember, you can always use Ctrl-z to undo what you just did if you want to try doing something different). Then move the cursor back to the first or second line and hit F4 twice to combine all of the lines into a single execution group.

The above steps for adding a line to an execution group are kind of tedious. Here is another way to do it. In the execution group at the end of this paragraph, place the cursor just to the left of the third prompt, between  the prompt and the bracket that marks the execution group. You can do this by either (carefully) clicking the mouse just to the left of the prompt, or by placing the cursor just to the right of the prompt and then using the left arrow key to move the cursor to the left side of the prompt (and one more strike of the left arrow key will move the cursor to the end of the second line). With the cursor to the left of the prompt, hit the Enter key. A new line will appear in the execution group just above the line where the cursor was (and if you hit Enter again, the commands in the execution group will be executed).

>    x^2-1;

>    factor( % );

>    expand( % );

x^2-1

(x-1)*(x+1)

x^2-1

Here is still a third way to add a line to an execution group. In the execution group at the end of this paragraph, place the cursor at the end  of the second line. Then, while holding down the shift key, hit the Enter key (so you are typing the Shift-Enter key combination). A new line will appear below the second line. But notice that this line does not have its own prompt on its left hand edge. This new line is really a continuation of the second line.

>    x^2-1;

>    factor( % );

>    expand( % );

x^2-1

(x-1)*(x+1)

x^2-1

Now you know how to combine lines into an execution group, split lines off of an execution group, and create new lines within an execution group. So let us turn to procedure definitions.

If a procedure definition is short, it can fit on a single line, even if it may have a few commands in it.

>    test := proc(n::integer) 13*n; factor(n); end;

test := proc (n::integer) 13*n; factor(n) end proc

But even when a procedure is short, it is usually better to enter the procedure definition on several lines to make the definition easier to read. So what we want to go over is how to create and then edit a multi line procedure definition.

The following prompt has the beginning of a procedure definition after it. Place the cursor anywhere in this line and hit the Enter key. Notice that Maple gives you a new prompt, and also a warning about a premature end of input. Just ignore the warning and type the first command from the above procedure definition (the command 13*n; ) after the new prompt. Then hit enter again. Maple gives you another new prompt, and the warning is still there. Enter the second command from the above procedure definition (the command factor(n); ) after the new prompt. Hit Enter once again and you get another new prompt (along with the warning). Now enter the closing of the procedure definition (the end; ) after the prompt and hit Enter one more time. The last enter causes Maple to digest the procedure definition.

>    test := proc(n::integer)

>   

Warning, premature end of input

If you did not do it while you were entering the procedure definition, go back and give the two lines in the body of the definition a little bit of indentation after the prompts (two or three spaces is enough). Proper indentation goes a long way towards making procedure definitions more readable.

The above exercise shows how easy it is to enter a procedure definition. Editing a procedure definition is no different than editing an execution group. If you want to enter a new line into the middle of a procedure definition, you can either use F3, Ctrl-j, and F4 to create the new line, or you can place the cursor to the left of a prompt and hit enter, or you can place the cursor at the end of a line and type Shift-Enter. Any one of these three methods will work and each one has its advantages and disadvantages.

The techniques mentioned here for working with execution groups and procedure definitions also work for the control statements discussed in the next chapter. In particular, if you want to create a multi-line for-loop or while-loop, you just do it the way we created a procedure definition. After you have typed the key word for  on a line, you can keep hitting the Enter key to create new input lines until you enter the closing key word od  (or end do ). Once Maple sees the closing keyword (with a semi colon or colon after it), it digests the definition and executes the loop. Similarly for while-loops and if-statements. And if you should nest one control statement inside of another, then Maple needs to see closing keywords (in the right order) for both of the control statements before Maple will digest and execute the commands.

>   

>