6.001 Recitation #3 – Feb 12, 2003

 

RI: Konrad Tollmar

http://www.ai.mit.edu/~konrad/6001/

 

Procedures and processes

Substitution model

Recursion veers iteration

1. Writing simple procedures.

Write procedure max that take two arguments and return the larger one.

 

2. Substitution model.

Imagen that we have defined the procedure y-x*x

(define x-y*y

        (lambda (x y)

          (- x ((lambda (x) (* x x)) y))))

 

 

Use the Substitution model to evaluate the following expression:

(x-y*y 11 3)

 

 

3. Iterative vers recursive

 

Define sum-iterative and sum-recursive that each take two arguments and calculate the accumulative sum between these arguments. E.g. (sum-iterative 2 4) ==> 9.

 

4. Iterative processes

 

Write a function count-up2 as count2, but as an iterative process.

 

(define (our-display x) (display x) x)

 

 

 

(define (count2 x)

 

(cond ((= x 0) 0)

 (else (count2 (- x 1))

                        (our-display x))))