CS 110: Elements of Computing. Instructor: Jim Aspnes


Homework Six

Due Friday, November 1st, 2002, at 5:00pm.

Solutions.

Your task

1. An imperative program

The following program fragment is written in the imperative programming language C. Describe (in English) what changes might occur in the machine code this program compiles to if the line int x; is replaced by float x;.

int x;
x = 2;
x = x+3;

2. A functional program

Below is a definition of a function written in the functional programming language Scheme. (See the notes linked to from the lecture schedule if you need to brush up on Scheme, but be aware that these notes use the word function where most dialects of Scheme use lambda.)

(define improve
    (lambda (f x)
	(if (> (f x) x)
	    (f x)
	    x)))
  1. What is the result of calling this function as (improve (lambda (x) (+ x 1)) 5)?
  2. What is the result of calling this function as (improve (lambda (x) (- x 1)) 5)?
  3. Give a succinct description (in English) of what improve does.

3. Choosing a paradigm

Suppose you want to write a program that contains representations of bank accounts that support operations like withdrawals, deposits, balance queries, and so forth, but you want to guarantee that under no circumstances will the balance in any bank account ever go below zero (e.g., by making withdrawals fail if they are too big). What programming paradigm would make this task easiest, and why?

Submitting your solution

Write up an email message containing your full name and the answer to these problems in plain text format (this means no HTML or Microsoft word documents), and send it to aspnes+110-02-6@cs.yale.edu. (Note: this is not the same email address as for previous homework.)


Fri 20 Dec 2002 16:31:12 EST hw6.tyx Copyright © 2002 by Jim Aspnes