CSC151.01 2009F Functional Problem Solving : Readings

The MediaScript Programming Environment


Summary: We examine the program development environment in which we will work for most of the semester.

Disclaimer: For this reading and the next, we have a bit of a “chicken and egg problem”. That is, it's difficult to introduce the environment in which you will write algorithms without first introducing the language in which you will write those algorithms. At the same time, you cannot start learning the language until you've learned a bit about the environment. In this reading, we will emphasize the environment, but teach you a bit about the language, too. In the paired reading, we'll teach you more about the language, but also a little bit about the environment. The lab should teach you a bit about both.

Introduction: Program-Development Environments

One of the main activities of many computer scientists is that of programming, expressing algorithms in a form understandable to the computer. (Even computer scientists who emphasize other issues often end up needing to build programs to help support arguments or to provide concrete evidence for theories.) Much of this semester, you will be writing programs as a way of learning about important concepts. Programming is also a skill that you can apply in a variety of contexts.

At this point, most programmers develop their code in what is called an program-development environment. Program-development environments let you write code, test bits of the code, format your code for easy readability, obtain documentation on built-in procedures, and so on and so forth.

In this class, we will use a program-development environment called MediaScript. MediaScript is an environment developed by a group of students and faculty at Grinnell. The primary goal of MediaScript is to support interactive scripting in Scheme. It is also designed to make it a bit easier for novices, and takes some user interface design from DrScheme, the program development environment we previously used at Grinnell. In this class, you will use a version of MediaScript that works within the GIMP.

Running MediaScript

Because MediaScript is a plug-in for the GIMP, you can only start it through the GIMP. As you may recall, you start the GIMP by clicking on the icon that you selected in the introductory Linux Lab. If you have no such icon, find a teaching assistant or teacher for help.

Once you're started the GIMP, you started MediaScript by selecting the Xtns menu, then the MediaScript menu item, and finally, Console.

An Overview of The MediaScript User Interface

You will find that MediaScript has many similarities to most applications, including an Edit that supports operations like cut and paste. You may, however, notice that MediaScript has a primary window that looks somewhat different than other programs.

As you'll note, that window has three panes. The top pane is called the Definitions Pane, the middle pane is called the Log Pane, and the bottom pane is called the Interactions Pane. As the names suggest, the top pane is used for writing definitions (more on those later), the bottom pane is used for your primary interactions with MediaScript, and the middle pane keeps a log of your interactions. (As you read through the materials for the course, you'll note that we format text you enter in the interactions pane and text you enter in the definitions pane slightly differently.)

We will start by exploring the interactions pane. For you initial use of MediaScript, you can treat the interactions pane as a fancy calculator. That is, you'll enter expressions and it will respond with the values of those expressions. For example, you can ask it to add 3 and 4, or to find the square root of 144.

Detour: A Bit of Scheme

The expressions you write in the interactions pane have to use the syntax of the Scheme programming language. Scheme uses a consistent syntax that makes a lot of sense to the computer, but that takes a bit for humans to master. (Fortunately, Scheme's syntax is much simpler than that of almost any other computer language.)

Here are the two key points to remember when writing Scheme expressions:

  • Non-trivial expressions are parenthesized: Every expression and subexpression (except for simple things like numbers or variables) must be surrounded by parentheses. Hence, to ask for the square root of 144 we would write (sqrt 144).
  • Expressions are in prefix order: Operations precede their operands. Hence, to add 3 and 4, we would write (+ 3 4) rather than the more traditional 3 + 4.

Why does Scheme use these two key ideas? Parentheses help Scheme resolve potentially ambiguities. For example, you'll note that different calculators compute different results for 3+4*5, depending on whether or not they are designed to accommodate precedence. In Scheme, since you must parenthesize every subexpression, you must write either (+ 3 (* 4 5)) or (* (+ 3 4) 5). Each expression indicates precisely what you want computed.

Prefix order, on the other hand, is intended to make life easier for the programmer. In particular, in languages in which some operations appear before their operands (such as sqrt) and others appear between their operands (such as + or modulo), programmers must remember or look up the order to use for each operation. In Scheme, there's never a question, because you always put the operation first.

The Interactions Pane

When MediaScript is ready for you to type an expression in the pane, it prints a greater-than sign (also known as a right angle bracket). You type an expression you want evaluated and then hit the Return key. If you have not closed all the open parentheses, it will let you type more on the next line. Once you have completed an expression, it will evaluate it and present the result.

> (+ 3 4)
7
> (sqrt 144)
12
> (string-append "Hello" " " "Sam")
"Hello Sam"

While the first two expressions are numeric, the last shows that MediaScript can work with words, too. (As long as those words are enclosed in non-curly quotation marks.)

Editing in the Interactions Pane

At times, you will find that you made a small typo in a long expression. For example, in computing the average of the grades 89, 66, 79, 85, 100, and 91, you might write

> (/ (+ 89 66 79 85 100 91) 5)
102

The result suggests, of course, that there was an error in the expression. (After all, the average of grades that are all 100 or less should not be more than 100.) A bit of analysis suggests that you really wanted to divide by 6, rather than 5. What can you do? You can retype the whole expression, but that takes a bit of time and gives you an opportunity to mistype something. You can cut and paste, and then edit the 5 (using the arrow keys and the backspace or delete keys).

Alternately, you can take advantage of MediaScript's Command History. MediaScript lets you scan through the previous expressions entered. To back up in the command history, press the up arrow key on the keyboard. To move forward, press the down arrow key.

The Definitions Pane

We've seen what the interactions pane is for, so what should we use definitions pane for? The simplest use is to assign names to values to make it easier to write our expressions. For example, if we wanted to work with grades on six homework assignments, we might name the grade on the first assignment grade1, the grade on the second assignment grade2, and so on and so forth. That way, the expression to compute the average grade would be the clearer

> (/ (+ grade1 grade2 grade3 grade4 grade5 grade6) 6)

In Scheme, we name values using the define operation, which we put in the definitions pane.

(define grade1 89)
(define grade2 66)
(define grade3 79)
(define grade4 85)
(define grade5 100)
(define grade6 91)

The definitions in the definitions pane are evaluated when you click the Run button. Once you've done so, you can use the names in the interactions window.

Saving and Restoring Definitions

If you spend a bit of time naming values, you may find it useful to save those definitions into a file that you can then restore later. As you might guess, you save definitions using a menu item from the Save. Since you can gather text in both the definitions pane and the log, MediaScript provides both Save Definitions and Save Interactions Log (as well as a host of variations thereof). Since the definitions are more likely to be of permanent use than the log , the default is to save definitions.

The custom is to save Scheme files with a suffix of .scm (or, sometimes, .ss). If you do not provide a suffix, MediaScript will provide one for you. (If you provide a suffix other than .scm or .ss, MediaScript will let you use that suffix, but you may find it harder to work with the file.)

Once you have saved definitions, you can quit MediaScript, go off and do other work, and then restart DrScheme and reload the definitions. As you might expect, you can load old definitions by using Open... or Open Recent from the File menu.

If you end up creating lots of definitions files and want to use them simultaneously (but don't need to edit them), there's a command you can type in the interactions window to get definitions from a file rather than from the definitions pane, load. You need to put the name of the file in quotation marks. For example,

> (load "mygrades.scm")

Concluding Thoughts

You have now learned enough to begin to interact with MediaScript. In a subsequent laboratory, we will ground these abstract instructions in concrete exercises.

Creative Commons License

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-9 Janet Davis, Matthew Kluber, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials copyright by John David Stone and Henry Walker and used by permission.)

This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.

This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.