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]

l4_1.png

l4_2.png

l4_3.png

l4_4.png

l4_5.png

Here is an example we have seen before, note that we are initializing our local x variable. We are also including local definitions.

l4_6.png

l4_7.png

l4_8.png

Sometimes we want to impose a local constant, we use the With[{list of local constants},expression]

l4_9.png

l4_10.png

l4_11.png

l4_12.png

We can use more than one constant.

l4_13.png

l4_14.png

l4_15.png

l4_16.png

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.

l4_17.png

l4_18.png

l4_19.png

The value of x is not stored globally.

l4_20.png

l4_21.png

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}].

l4_22.png

We can use a wide variety of controls.

l4_24.png

Here is a setter bar taken from the documentation.

l4_26.png

Here we label each button.

l4_28.png

We can use a popup menu.

l4_30.png

Here is an example of using Locators from the documentation.

l4_32.png

Here we label each locator.

l4_34.png

Here we allow new locators to be added using [CTRL][ALT] Left Click.

l4_36.png

Here we introduce a two-dimensional slider.

l4_38.png

We return to our original example and put in an input field.

l4_40.png

We can apply default values and labels.

l4_42.png

We can make it so that the phase updates dynamically.

l4_44.png

Animate

We can also animate a process by writing Animate[expression using a variable name,{variable name,minimum value,maximum value}].

l4_46.png

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 l4_48.png.

l4_49.png

l4_50.png

ListAnimate

If we want to animate the elements of a list we write ListAnimate[{list}].

l4_52.png

l4_53.gif

Dynamic

If we want a variable to change whenever we change its content as we change it we write Dynamic[expression].

l4_54.png

l4_55.png

l4_56.png

l4_57.png

We can make it interactive by assigning it to a slider.

l4_58.png

l4_59.png

We are not restricted to a slider.

l4_60.png

StringTake[, -1 + System`Convert`HTMLDump`pos1$426530]