CSC151.01 2009F Functional Problem Solving : Readings
Primary: [Front Door] [Syllabus]  [Academic Honesty] [Instructions]
Current: [Outline] [EBoard] [Reading] [Lab] [Assignment]
Groupings: [Assignments] [EBoards] [Examples] [Exams] [Handouts] [Labs] [Outlines] [Projects] [Readings]
References: [Primary] [AZ]  [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [CSC151.02 2009F (Weinman)] [CSC151.02 2009S (Davis)] [CSC151 2008S (Rebelsky)]
Misc: [SamR] [MediaScript] [GIMP]
Summary: We consider the purpose of Scheme and the structure of expressions in Scheme.
Our main objectives in this course are (1) to learn about algorithms, stepbystep methods for solving problems, and (2) to learn how to direct computers to perform such algorithms for us. A programming language, such as Scheme, is a formal notation in which one can express algorithms so exactly that a computer can perform them without any other assistance from human beings. The expression of an algorithm in such a notation is called a program, and the computer is said to be executing the program when it is performing the algorithm as directed.
Although not all of the problems that we'd like computers to solve are arithmetical, the simplest examples belong to that category, and we'll start with a few of them. Here, for instance, is a program, written in Scheme, that directs the computer to find the answer to the question “What is the square root of 137641?”
(sqrt 137641)
In order to make the Scheme environment answer that question, you need to learn how to work with Scheme. There are many different implementations of Scheme available. We'll use MediaScript, which you will be learning simultaneously. In the MediaScript environment, which you will use to develop your programs, you can work interactively with Scheme, typing a bit of code, finding a result, typing another bit of code, finding a result, and so on and so forth.
>
(sqrt 137641)
371
The full Scheme language contains several hundred primitive
procedures  operations, such as finding the square root of
a number, for which a Scheme interpreter can use prepackaged algorithms.
MediaScript includes implementations of many of those procedures. Some
programmers who are experts on square roots and on the idiosyncrasies
of our computers have figured out and written up a stepbystep
method for computing the square root of any number, using only the very
elementary transformations that the processor can perform. Most Scheme
interpreters recognize
as the
name of this algorithm and knows where the processor instructions that
carry it out are stored. When the interpreter receives a command to
compute a square root, it recovers these instructions and arranges
for the processor to follow them.
sqrt
A procedure call is a command that directs the
Scheme interpreter to activate a procedure such as sqrt
.
(Note:
is the name of the
procedure, and sqrt
(
is
the procedure call.) In Scheme, every procedure call begins with
a left parenthesis and ends with the matching right parenthesis.
Within the parentheses, one always begins by identifying the
procedure to be called and then continues by identifying the
arguments, the values that the procedure is
supposed to operate on. The sqrt
137641)
procedure takes only one argument, the number of which you want the
square root, but other procedures take two or more arguments, and some
need no arguments at all.
sqrt
All arithmetic in Scheme is done with procedure calls. The primitive
procedure
adds numbers together,
the primitive procedure +
subtracts
one number from another. Similarly, the primitive procedure

performs multiplication, and
the primitive procedure *
performs
division. The fact that in a procedure call the procedure is identified
first makes calls to these procedures look different from ordinary
arithmetic expressions: For instance, to tell DrScheme to subtract
68343 from 81722, one gives the command /
(
. Why someone might want to compute that value is
left as an exercise for the reader.

81722 68343)
Other Scheme procedures include
,
and abs
. The Scheme procedure
expt
abs
computes the absolute value of its argument.
The Scheme procedure for raising a number to some power is
.
expt
As you may have noted, the appropriate way to write a Scheme expression is
(operation
operand_{1}
...operand_{n}
)
That is, you parenthesize the expression (and any nontrivial subexpressions) and you place the operation before the operands.
It is harmless, though unproductive, to try to give a Scheme interpreter ordinary arithmetic expressions, in which the procedure is written between the operands.
But what if you want to compute something more complex than a simple sum, product, difference, or whatever? Sometimes, you want to combine operations. For example, to find the average of four numbers, we need to both sum the numbers and divide the result by four. One strategy would be to compute the sum, copy that number, and then insert it into an expression that divides by four.
However, Scheme provides a much more sensible solution: You can nest expressions, one inside another. When Scheme encounters nested expressions, it evaluates the inner expressions first. For example, consider the expression
(/ (+ 90 80 10 90) 4)
tells the Scheme interpreter to first add 90, 80, 10, and 90, and then to divide the result by 4. Similarly,
(+ (* 80 0.45) (* 95 0.35) (* 100 0.20))
tells the Scheme interpreter to multiply 80 by 0.45, to multiply 95 by 0.35, to multiply 100 by 0.20, and to sum the results. Interestingly, the Scheme interpreter is free to do the multiplications in any order, as long as it completes all three before it does the sum. For example, it might first multiply 100 by 0.20 or it might first multiply 80 by 0.45. (MediaScript tends to evaluate subexpressions from lefttoright.)
MediaScript's Scheme interpreters, like most versions of Scheme, can also learn new names for things by reading definitions. Here's what a definition looks like:
(defineFor example,name
value
)
(define daysinaweek 7)
Like a procedure call, a definition begins and ends with matching
parentheses. To distinguish between definitions and procedure calls,
the Scheme interpreter looks at what comes immediately after the
left parenthesis. In a definition, the keyword define
must appear at that point. The keyword define
is
not the name of a procedure; it is part of the
syntactic structure of the Scheme programming language. Its only role
is to serve as the mark of a definition.
After the keyword define
, a definition contains the
name being defined and an expression that identifies the value
that the name should stand for. In this example, the name is
daysinaweek
. (Notice that in Scheme a name can, and
often does, contain hyphens internally.) The value that it names is
the number 7. Once MediaScript's Scheme interpreter has seen this
definition, it remembers that daysinaweek
stands for 7.
The value that gets a new name need not be a number; it can be
anything, even a computation or the name of a procedure. For example,
if you don't like the name
for
the multiplication procedure and would rather call it by the name
*
, just start each sequence
of interactions with MediaScript by giving it the definition multiply
(define
multiply *)
. (Alternately, place the definition in a file,
and load the file in your interactions pane or definitions pane.)
At this point, we hope you're wondering what other useful and interesting procedures are built into Scheme. Section 6.2.5 of the Revised^{5} report on the algorithmic language Scheme contains a list of the ones that are mainly about numbers, and that's only one section of the full roster of standard Scheme procedures. Fortunately, most of the primitive procedures perform small, simple jobs and are easily learned.
Primary: [Front Door] [Syllabus]  [Academic Honesty] [Instructions]
Current: [Outline] [EBoard] [Reading] [Lab] [Assignment]
Groupings: [Assignments] [EBoards] [Examples] [Exams] [Handouts] [Labs] [Outlines] [Projects] [Readings]
References: [Primary] [AZ]  [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [CSC151.02 2009F (Weinman)] [CSC151.02 2009S (Davis)] [CSC151 2008S (Rebelsky)]
Misc: [SamR] [MediaScript] [GIMP]
Copyright (c) 20079 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. CCLI0633090. 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
AttributionNonCommercial 2.5 License. To view a copy of this
license, visit http://creativecommons.org/licenses/bync/2.5/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.