Lab 3

Goal:

Learn to write a higher-order procedure that returns a procedure.

NOTE: To allow the tester to distinguish between regular and honors students, assign a value to the symbol student as instructed below.

Experiment for CISC280-010 students

Put the line (define student "regular") at the top of your submission so that the tester will expect you to do the following experiment.

Let f and g be functions taking one argument. The composition of f after g is the function h such that h(x) = f(g(x)). Define a procedure compose that takes two other procedures as its arguments and returns a procedure that is effectively the composition of the first procedure argument after the second procedure argument. In Scheme notation, you should be able to do the following after having defined compose:

(define h (compose f g))

(h 3)
where f and g are one-argument procedures such as square and inc (defined in class and in the text). The value of (h 3) will be the same as if you had evaluated (f (g 3)).

The composition procedure can also be used without giving it a name. For example,

((compose square inc) 6)
returns 49, and
((compose inc square) 6)
returns 37.

Hint: You will want to use a lambda expression to create the composition procedure. It should describe what value the composition will return for a given input (to the lambda-generated procedure).

Submit your definition of compose to the Automated Tester and click on the Confirm butten when you get a PASS.

Experiment for CISC280-080 students

Put the line (define student "honors") at the top of your submission so that the tester will expect you to do the following experiment.

For this lab assignment, you will implement a simple procedural representation of numbers so that we can do arithmetic with infix notation.

Define the function make-num of one argument so that it returns a procedure that accepts appropriate messages. Also define a function value that allows us to extract the number from that procedure. Here are the requirements in more detail.

The function make-num takes a number (integer or real) as its argument and returns a procedure. This procedure can take one or two arguments. If the first argument is 0, it returns the number that was input to the call to make-num when the procedure was created. If the first argument is +, it gets the second argument, which is also supposed to be a procedure representing a number, adds its own number to the value of that number, and returns a procedure that represents the sum. Similarly, if the first argument is *, it gets the second argument and returns a procedure that represents the product of its own number with the number represented by the second argument.

The following interactions should give you an idea of what these two functions do.

> (define seven (make-num 7))
> (define five (make-num 5))
> (value seven)
7
> (value five)
5
> (define sum (seven + five))
> (value sum)
12
> (value (five * seven))
35
>
Hint: Use eq? for all your testing of messages. It works for numbers as well as pointers. I suggest that you use a cond expression rather than an if expression. Don't forget the utility of the dot notation when describing procedures with multiple arguments.

Submit your definitions of make-num and value in one submission to the Automated Tester and click on the Confirm butten when you get a PASS.