Creating New Functions Using Scoping Constructs and Interactive Functions
George E. Hrabovsky
MAST
Introduction
In this lesson we will introduce two topics for creating new functions in MMA/WL. First will be scoping constructs. Second will be interactive functions.
We will begin with describing what scoping means. We will then discuss the Module command and the Block command.
We then turn to interactivity. After describing what we mean by that we will examine the Manipulate and Dynamic commands. We then turn to the hybrid scoping interactive command DynamicModule.
Scoping
There are many definitions of scope in computer science that relate to the binding of names to parts of the program where that name is valid (called lexical scoping). Another relates the binding of names to the state that a program is in (called dynamic scoping). In MMA/WL we use a name binding within a block of code, this is called local scoping.
Module
To make a program with local lexical scoping we use the Module command. We write Module[{local variable list},body]
Here is an example we have seen before, note that we are initializing our local x variable. We are also including local definitions.
Sometimes we want to impose a local constant, we use the With[{list of local constants},expression]
We can use more than one constant.
Block
If we want a program the keeps the names of variables global but the values are local, local dynamic scoping, then we use Block. We write Block[{list of local values},body]. Here we need to put in an expression involving x as an argument, what we call b.
The value of x is not stored globally.
Interactivity
MMA/WL has developed commands that allow you to quickly and efficiently create GUIs. These can be made arbitrarily sophisticated and pleasing to the eye. With free software allowing users to operate programs created this way, Wolfram Research has opened the door to making online education work as well as classroom instruction.
Manipulate
If we want to create a quick and dirty GUI we can use Manipulate. We write Manipulate[expression using a variable name,{variable name,minimum value,maximum value}].
We can use a wide variety of controls.
Here is a setter bar taken from the documentation.
Here we label each button.
We can use a popup menu.
Here is an example of using Locators from the documentation.
Here we label each locator.
Here we allow new locators to be added using [CTRL][ALT] Left Click.
Here we introduce a two-dimensional slider.
We return to our original example and put in an input field.
We can apply default values and labels.
We can make it so that the phase updates dynamically.
Animate
We can also animate a process by writing Animate[expression using a variable name,{variable name,minimum value,maximum value}].
As with Manipulate there are many types of controllers we can use.
Here is a very nice example from the documentation where we begin a with a function to calculate the roots of the polynomial .
ListAnimate
If we want to animate the elements of a list we write ListAnimate[{list}].
Dynamic
If we want a variable to change whenever we change its content as we change it we write Dynamic[expression].
We can make it interactive by assigning it to a slider.
We are not restricted to a slider.
StringTake[, -1 + System`Convert`HTMLDump`pos1$426530] |