! Ecstasy Lsd

Transcription

! Ecstasy Lsd
Carleton University
School of Computer Science
COMP 3007 Assignment 1 (F 13)
Due: Mon, Oct 7, 11:55pm (at the very latest!!!!)
The objectives of this assignment are to familiarize you with Scheme, to build abstractions with procedures, to use recursion, and to
explore patterns of computational processes. You should follow the programming style and guidelines set forth in class. In particular,
use meaningful names for variables and procedures, use recursion wherever possible and write concise, readable procedures. It is up to
you to make up a convincing, documented test plan.
Do not use set! or any procedure with ! in your solutions.
NOTE: Please read the guidelines on the course website for documentation and testing.
Question 1
(a) Write a Scheme procedure to evaluate the expression: 2*4+(3*5+6*7)*9.
(b) Write a Scheme procedure to evaluate the expression: -1+(3+4*2-6)*3/(7*2).
Question 2
A function f is defined by the rule that f(n) = n if n<4 and f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) + 4f(n - 4) if n>= 4. Write a procedure
that computes f by means of a recursive process. Write a procedure that computes f by means of an iterative process.
Question 3
Write an iterative procedure power-close-to with 2 positive integer arguments b and n, that returns the smallest power of b > n, i.e.
it returns e such that be > n.
Use the built-in (expt b e) to compute be.
Embed the helper function inside power-close-to and take advantage of lexical scoping to remove unnecessary parameters from the
embedded function.
For example, (power-close-to 2 1000) => 10 because (expt 2 10) => 1024, and (power-close-to 2 150) => 8
because (expt 2 8) => 256 but (expt 2 7) => 128.
Question 4
Design a procedure that evolves an iterative exponentiation process that uses successive squaring and uses a logarithmic number of
steps to compute bn. A simple, linear process would just multiply b with itself n times. However, by observing that (bn/2)2 = (b2)n/2 we
can perform the calculation in a logarithmic number of steps. NOTE: YOU ONLY HAVE TO HAVE YOUR PROCEDURE WORK
FOR NON-NEGATIVE INTEGERS.
Question 5
Design a procedure called multiply that performs multiplication of two integers by successive addition; i.e. you may not use the
built-in multiplication function. The procedure should be formulated using recursion. In your associated documentation give an
example of the substitution model for (multiply 2 3).
Question 6
Using the solutions to questions 4 and 5, devise a procedure that generates an iterative process for multiplying two integers in terms of
adding, doubling, and halving and uses a logarithmic number of steps. In your associated documentation give an example of the
substitution model for (multiply 2 3).
Question 7
Ben Bitdiddle has invented a test to determine whether the interpreter he is faced with is using applicative-order evaluations or
normal-order evaluation. He defines the following two procedures:
(define (p) (p))
(define (test x y)
(if (= x 0)
0
y))
He then evaluates the expression:
(test 0 (p))
What behavior will Ben observe with an interpreter that uses applicative-order evaluation? What behavior will Ben observe with an
interpreter that uses normal-order evaluation? Explain your answer. (Assume that the evaluation rule for the special form ‘if’ is the
same for both evaluation schemes: The predicate expression is evaluated first, and the result determines whether do evaluate the
consequent or the alternative expression.
Question 8
Newton's method for cube roots is based on the fact that if y is an approximation to the cube root of x, then a better approximation is
given by the value:
(x/y2+2y)/3
Use this formula to implement a cube-root procedure analogous to the square-root procedure from the lecture notes.
Question 9
Observe that our model of evaluation allows for combinations whose operators are compound expressions. Use this observation to
describe the behavior of the following procedure:
(define (a-b a b)
((cond ((> b 0) +) ((= b 0) -) (else *)) a b))
Your answer should describe what happens for all integer values of a and b. Illustrate your answer using the substitution model.
Question 10
(a) Using Newton's approximation for square root finding as shown in the Scheme notes modify the block structured form of the
algorithm in order to allow a user-defined procedure for good-enough? to be used in the algorithm. The user-defined procedure should
be passed into the square-root-finding procedure.
Demonstrate in your test cases that you can calculate the (my-sqrt 2) to varying accuracy.
(b) Extend the solution provided in (a) to force the termination of the algorithm after a maximum number of iterations.
(c) Create a new-if procedure as below:
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
Replace the use of if in sqrt-iter with new-if. Does the new version work? Explain.
Submission
Your assignment must be submitted electronically using the cuLearn system.
Your assignment should include a README.TXT file that contains details of the testing you have performed and any special
requirements that you have for setup. Assignments that are not adequately commented will have marks deducted. Your name and
student number should be included in a comment block at the top of each file that you submit. All Scheme files should have the
extension either .rkt or .scm. All text files should have the extension .txt.

Similar documents