Scheme Application Lab

Procedure Applications

One important benefit of Scheme is that functions can be passed as arguments to other functions and can be returned from other functions with ease.

Functions as First-Class Data Objects

A first-class data object is one that has no restrictions on its usage. Integers exemplify first-class data objects because of the several ways integers can be used as

1. Components in expressions

2. Elements in an array

3. Values bound to identifiers

4. Arguments to functions

5. Values returned by functions

• Many languages do not allow a function to return another function or the creation of arrays of functions or any other first-class data object freedoms.

• Several LISP dialects, including common LISP, allow functions to be passed as arguments to other functions. It is more difficult to pass common LISP functions as arguments because some extras are required. For example, it may be necessary to apply extra function evaluations or quote a function name.

• Scheme however, treats functions as first-class data objects, where functions can be used in the same ways as the integer type above. By allowing functions to have the same status as other data objects, Scheme achieves a degree of consistency and elegance that is not present in other languages. One example is defining variables and defining functions are handled in the same way.

(define x 3) ; variable

(define y (lambda (n) ...)) ; function

Now if x or y is evaluated, a first-class object is returned and can be used in several ways. In Scheme, the same technique is used for passing variables and functions as arguments. Passing arguments uses the call-by-value technique where

all the arguments are evaluated, and

the resulting values are passed to the called function.

For example using the definitions above, the function call ... (z x y) ...

z would be called with the values 3 and (lambda (n) ...)

because x evaluates to a constant and y to a procedure

 

Functions as Arguments

In computer science when the same operation needs to be performed on several sets of data, the routine operations can be abstracted as a procedure. In mathematics, functions provide a similar abstraction mechanism. Scheme supports this function abstraction. Consider a large programming assignment that can logically be subdivided into several separate tasks where some tasks require other tasks as input (one task is a parameter of another). Even though task x is viewed as input to operation z, many languages force us to call task x from some point inside operation z. Scheme allows parallel logical organization making the algorithm(s) more straightforward. As a result, the long-term utility of the software is enhanced by increasing the feasibility of software update operations. Consider the following code:

(define (square x) (* x x))

(define (cube x) (* x x x))

(define (summation formula start stop)

(do ((i start (+ i 1))

(sum 0 (+ (formula i) sum)))

((> i stop) sum)))

Procedure summation takes some task as input plus data in the form of start and stop variables and computes the sum of the squares of integers between start and stop inclusive. Try the following function calls.

(square 3)

(cube 3)

(summation square 1 3)

(summation cube 1 3)

 

You must turn in the above function on disk as well as a hardcopy of results from evaluating the above expressions.