03 Input Assistance
0 (0 Likes / 0 Dislikes)
This section of Introduction
to Wolfram Notebooks
is about notebook features
for entering programs
and for making programs
more readable.
As a simple example,
here is a short program
based on the manipulate function
and as this program
is being entered,
mismatched brackets
and incomplete command names
and anything else that is
even temporarily, a syntax error
is shown in a different color.
This particular program gives
a slider for adjusting a parameter
in an image processing operation.
A good way to check
that a program like this
has been entered correctly
is to look for anything
that is displayed
in a color other than black.
Any name that doesn't
have a definition
is shown in a different color.
So for example, if the name of
a command is spelled incorrectly,
like the name in this example,
that name will be shown
in a different color.
Color is also used to show
a syntax errors.
For example, the unmatched
brackets in this input
are shown in a different color.
You can use notebook options
to make the different colors
easier to see.
For example, here is that
notebook with option settings
to show local variables,
like the variable r
in the Manipulate function,
in bright green,
undefined names, like
the incomplete name shown here,
in large underlined
magenta characters;
and syntax errors,
like unmatched brackets, in red.
There are several
tools specifically
to help with getting all
of the brackets and braces
and other delimiters to match up.
For example,
when a closing delimiter
such as a closing bracket
is entered,
the opening delimiter flashes
briefly to indicate the matching.
So in this input, as soon as
the closing bracket is entered,
the opening bracket
flashes briefly
and the color changes to black.
Here is that same input
with the colors
set back to their defaults,
which again shows the opening
delimiters flashing briefly
as soon as the closing delimiters
are entered.
Another way to find
matching brackets
is to click anywhere
between matching brackets,
which causes the nearest
enclosing brackets
to be highlighted.
A similar feature is the Check
Balance item under the Edit menu.
Positioning the cursor anywhere
between matching delimiters
and choosing Check Balance,
or the keyboard shortcut,
has the effect of selecting
everything between
and including
the matching delimiters.
Choosing Check Balance again
expands the selection out
to the next nearest
enclosing delimiters.
Another related feature
is repeated clicking,
which can be used
to select subexpressions.
For example, double-clicking
the EdgeDetect command,
selects the command name
and clicking three times
selects the command
as well as the arguments
and the enclosing square brackets.
In this example,
clicking four times
selects the arguments
of the Manipulate command
and clicking five or more times
selects the entire input.
These features for finding
subexpressions
and matching brackets
are especially useful for commands
that are many lines long,
where it can be difficult to locate
the beginning and the end
of a command.
There are also several ways
of using formatting
to help with getting
the syntax right
and to improve
the readability of programs.
A traditional method
that has been used
since the early days
of computer programming
is indentation.
For example, that program
could be formatted like this,
which puts matching delimiters
at similar levels of indentation.
Another formatting tool
is conventional
mathematical notation,
which can be useful
in programs that include
any sort of mathematics.
For example, here is an input
to the Erosion function
where the second argument
is a matrix.
But rather than showing
the matrix in a linear form,
with nested curly braces
as in this input,
that matrix can be shown in
traditional mathematical notation
by selecting the Cell
and choosing TraditionalForm
from the Convert To submenu
under the Cell menu,
which makes that part of the input
more recognizable as a matrix.
Here is another example,
this time involving a program
from signal processing.
A key detail of this program
is this bit of mathematics,
which is entered here
in linear programming notation,
but which can be made
more recognizable
by formatting that formula
as a fraction
using either StandardForm
or TraditionalForm
from the Convert To menu.
One other formatting tool that
can be useful in larger programs,
especially programs
much larger than this one,
is to iconize parts
of the program.
For example, that mathematical
formula can be replaced
by what is basically
a labeled button
by using the iconize function.
The iconize form
can be copied and pasted
like any other expression,
and in a program it behaves
just like the original expression.
You can also iconize
selections in a program.
For example, in this program
you can triple click the name
of the plotting function
to select the part of this program
that generates the plot
and then choose
Iconize Selection
under the Edit menu to replace
that selection with an icon.
There is also a keyboard shortcut
for iconizing a selection,
and on many platforms
there is a context menu
for selections
that can be opened,
typically by ALT clicking or
CONTROL clicking on a selection.
For example, after selecting part
of the program,
this shows that context menu
for the selection,
which includes Iconize
as one of the items in the menu.
In a program with expressions
that have already been iconized,
there are several ways to see
the original expressions,
one of which
is to click the button
with the plus sign
in the iconize form
and choose Uniconize.
As already noted,
iconized forms like this
are especially useful
for larger programs,
where the iconized forms
provide a nice way to manage
long blocks of code,
as small labeled pieces.
There are many other features
to help with programming
that go beyond the features
that were covered in the section.
Often just clicking a button
that pops up
is a good way
to find useful features.
For example, the error message
from this input
gives a button that you can click
to open a Stack Trace
to see where
the message came from,
which can be useful
for debugging.
Also the focus in this section
is on Wolfram Notebooks,
but there are
many programming tools
that are part of the underlying
computation engine.
For example,
the Echo function in this input
shows an easy way to monitor
the progress of a calculation.
That's the end of the examples
for this section.
For the features that were
covered in this section,
you can find more information
in the Wolfram Documentation,
and in particular, you can find
more information about syntax,
coloring, bracket matching
and related topics
in this tutorial on using
the Input Assistant.