EIWL32AVTake1-SD
0 (0 Likes / 0 Dislikes)
Section 32 of Stephen Wolfram's book <i>An Elementary Introduction</i>
<i>to the Wolfram Language</i> is about patterns, which are a fundamental
part of the language. Most Wolfram Language calculations, in fact,
are done by finding a pattern that matches the input and then doing
the operations that have been set up for things that match that pattern.
One basic pattern is the pattern that matches anything, which is entered
as a single underscore or "blank" character. Like all patterns, the blank
pattern can be used as part of a larger pattern. For example, this list
is a pattern that matches a list with two elements
where the elements can be anything.
One use of patterns is in the Cases function, which is a function for
picking things out of a list, kind of like the Select function, but the
Cases function picks out things that match a pattern. For example, here is
a list of lists, and this uses the Cases function to select
from that list the elements that are lists with two elements.
The first argument in the Cases function is the list from which to
select things, and the second argument is the pattern to use for matching.
Here is another pattern that uses a blank pattern. This pattern matches a
list with two elements where the first element is the number 1 and the
second element can be anything. And this shows the Cases
function picking out things that match that pattern.
Another common pattern is one that is entered by putting
a vertical bar between two or more alternatives. For example, this
pattern, with a vertical bar between 1 and 2, will match either 1 or 2.
This list is a pattern that uses the vertical bar and a blank.
This pattern will match a list with two elements where the first element
is a 1 or a 2, and the second element can be anything.
And this shows the Cases function picking out things that match that last pattern.
One last bit of pattern vocabulary in this section is the double-blank
pattern, which is entered with two underscore characters.
The double-blank pattern matches any sequence of one or more things,
so, for example, here is a pattern that will match any list with two or more
elements and where the last element is this blue color swatch.
Here is a more complicated pattern that will match any list where the first
element is a 1 or a 2, and there is at least one element in the middle,
which can be anything, and the last element is the blue color swatch.
This shows the Cases function picking out elements that match that pattern.
So far, all of the patterns have been lists, but they don't have to be.
For example, here is a pattern with a function <i>f</i> and the double-blank pattern.
This pattern will match a function <i>f</i> with one or more arguments,
and this shows the Cases function selecting things that match that pattern.
One of the most common uses of patterns is in operations that involve
replacing one thing by another thing, or in replacing parts of things.
In the Wolfram Language, replacements are defined using rules.
For example, here is a rule for replacing "b" by Red.
The rule is entered by typing the left side of the rule, followed by a dash
and a greater-than character and then the right side of the rule.
As soon as you finish entering the dash and the greater-than character,
that part of the input is automatically displayed as an arrow pointing to the right.
The actual replacement can be done using the /. operator. For example,
here is that rule used to replace all of the "b"s in a list by Red color swatches.
Where patterns come in is that the left side of the rule is a pattern,
and when the rule is applied, everything that matches that pattern will be replaced.
Even the letter "b" on the left side of this rule is a pattern—
it's just that it's a trivial pattern that matches only the letter "b".
Here we will mostly consider more interesting patterns. For example,
here is a rule where the left side of the rule is a pattern that will match any list with two
elements and where the first element is 1 and the second element can be anything.
When that rule is applied using the /. operator, everything
that matches that pattern will be replaced by an "x".
Here is a rule where the left side of the rule is a pattern that matches any
list with two or more elements and where the last element is Blue.
And this shows that rule used to replace matching elements by "y".
You can also apply both of those rules at the same time
by putting them in a list after the /. operator.
Often, when making replacements, it is helpful to give names to pieces
of the thing that match the pattern, so that those pieces
can be used elsewhere in the replacement.
For example, here is a rule where the pattern includes an "x" before the blank.
The blank still matches anything, and the "x" gives a name to the
matching thing so that it can be used in the replacement.
This shows that rule used to do a replacement with the /. operator.
In this result, all of the lists with a 1 followed by a second element
have been replaced by that second element plus 10.
If the second argument in the Cases function is a replacement rule, then
matching things will be replaced by the thing on the right side of that rule.
If a named pattern like "x blank" occurs more than once in a pattern,
the matching things have to be identical. For example, this pattern
matches a list with two elements where the elements have to be the same,
and this shows that pattern used in the Cases function
to pick out elements that match that pattern.
That's the end of the examples for Section 32. To summarize, here is
the programming vocabulary from this section.
The examples introduced the notion of a pattern. One simple pattern is
the one that is entered as an underscore character—the "blank"
pattern—which is the pattern that matches anything. The Wolfram Language
has a rich vocabulary with many other tools for specifying patterns,
and the "blank" pattern and the other patterns in this
section are a few of the parts of that vocabulary.
The examples also showed the use of patterns in replacement rules,
which is probably the most common use of patterns in the Wolfram Language.
You can find more information about patterns by selecting
Wolfram Documentation under the Help menu to bring up the Documentation
Center page, clicking on the Core Language & Structure button and
then selecting Rules & Patterns, which brings up this page with links
to more information about rules, as well as this link to the page with
more information about patterns, starting with the "blank" pattern
and all of the other patterns from this section.
Here are the exercises from the end of Section 32.
These exercises are all good examples for getting some practice
coming up with patterns to match things. For example, exercise 32.6,
"Remove the vowels a, e, i, o and u from the list of characters in 'The Wolfram Language'."
There are lots of ways to do that. One way to get started is to use the
Characters function to separate the string into individual characters, and
then apply a rule for replacing vowels. This shows a rule that replaces the letter "a" with Red.
So one solution is to use a separate rule for each vowel,
like this for replacing the vowels "a" and "e".
Another approach is to use the vertical bar pattern like this to get a
pattern that will match any of several alternatives,
and then use that pattern in a rule to make the replacements.
The exercise actually asked for the vowels to be dropped, which can be
done by replacing them with Nothing, so change Red to Nothing and the
vowels are dropped from the list. And to finish the exercise, the characters
can be combined back into a single string using the StringJoin function.
Thank you for watching. I hope that was useful, and I hope you will be
successful in using patterns and replacement rules in your own programs!