COSC 3353 Programming Assignment 1

Scheme

Purpose: This assignment gets you started using Scheme by looking at basic operations

Due: Beginning of class on February 4. You will not be doing a code review on this assignment.

Requirements: This is an individual assignment. You may not work with other class members or share code. All code is to be written by the individual student and may not be downloaded from the Internet or other sources. This assignment was adapted from projects given in MIT Course 6.001. Do the following:

Part A. Scheme Expression Evaluation Scheme programming consists of writing and evaluating expressions. The simplest expressions are things like numbers. More complex arithmetic expressions consist of the name of an arithmetic operator (things like +, -, *) followed by one or more other expressions, all of this enclosed in parentheses. So the Scheme expression for adding 3 and 4 is (+ 3 4) rather than 3 + 4.

While in the *scheme* buffer, try typing a simple expression such as

1375

Typing this expression inserts it into the buffer. To ask Scheme to evaluate it, hit enter, which causes the interpreter to read the expression immediately preceding the cursor, evaluate it (according to the kinds of rules described in lecture) and print out the result which is called the expression's "value". Try this.

An expression may be typed on a single line or on several lines; the Scheme interpreter ignores redundant spaces and line breaks. It is to your advantage to format your work so that you (and others) can read it easily. It is also helpful in detecting errors introduced by incorrectly placed parentheses. For example, the two expressions

(* 5 (+ 2 (/ 4 2) (/ 8 3)))

(* 5 (+ 2 (/ 4 2)) (/ 8 3))

look deceptively similar but have different values. Properly indented, however, the difference is obvious.

(* 5

(+ 2

(/ 4 2)

(/ 8 3)))

(* 5

(+ 2

(/ 4 2))

(/ 8 3))

While the Scheme interpreter ignores redundant spaces and carriage returns, it does not ignore redundant parentheses! Try evaluating

((+ 3 4))

Scheme should respond with a message akin to the following:

function call: expected a function after the open parenthesis, but received 7

Part B. Below is a sequence of Scheme expressions. Can you predict what the value of each expression would be when evaluated? Go ahead and type in and evaluate each expression in the order it is presented. Extract the relevant parts out of the buffer and copy this into a text file for submission.

-37

(* 3 4)

(> 10 9.7)

(- (if (> 3 4)

7

10)

(/ 16 10))

(* (- 25 10)

(+ 6 3))

+

(define double (lambda (x) (* 2 x)))

double

(define c 4)

c

(double c)

c

(double (double (+ c 5)))

(define times-2 double)

(times-2 c)

(define d c)

(= c d)

(cond ((>= c 2) d)

((= c (- d 5)) (+ c d))

(else (abs (- c d))))

;;; PART 2

;;;

;;; The following test cases explore the evaluation of simple expressions.

;;;

(* 7 8) ;

Value: 56

Help: I will be available during office hours to provide assistance with this program and in using Scheme.

Grading Criteria. Submit your text file as an attachment to an email to me. The subject line should be COSC 3353, Assignment 1, Your Name.

Grading Criteria: 50 points available for this programming assignment.