| Tutorial 1: Solutions (Sep 14, 2007)- Simple Functions in Scheme
- Scheme notation vs. Mathematical notation
From Scheme to Math:(define (f x) (- (* 5 x) 4)) ⇒f(x) = 5x-4(define (g x) (- (/ (* (+ x 2) (- x 3)) 5) x)) ⇒g(x) = x(x + 2)(x - 3) / 5 From Math to Scheme:- f(x) = (x+5)/(8x-9)⇒
(define (f x) (/ (+ x 5) (- (* 8 x) 9)) - g(x,y) = (xy-7)(x+y) - (3x + 5y - 12)
⇒(define (g x y) (- (* (- (* x y) 7) (+ x y)) (+ (* 3 x) (- (* 5 y) 12)))
- Function Evaluation
Note the following steps are to illustrate the intermediate results only and do not necessarily follow the exact order of the evaluation steps performed by DrScheme.(- (* 4 5) (/ 6 3)) ⇒(- 20 2) ⇒18 (/ (+ 2 (- 5 2) (* 3 3)) (/ (- (* 5 5) 4) 3)) ⇒(/ (+ 2 3 9) (/ (- 25 4) 3)) ⇒(/ 14 (/ 21 3)) ⇒2 (define (f x) (* x (+ x 1)))
(+ (f 1) (/ (f 2) 3)) ⇒(+ (* 1 (+ 1 1)) (/ (* 2 (+ 2 1)) 3)) ⇒(+ 2 (/ 6 3)) ⇒4 (define (f x) (- x 1))
(define (g x) (* x (f (/ x 3)) 2))
(/ (g (g 6)) 2) ⇒(/ (g (* 6 (f (/ 6 3)) 2)) 2) ⇒(/ (g (* 6 (f 2) 2)) 2) ⇒(/ (g (* 6 (- 2 1) 2)) 2) ⇒(/ (g 12) 2) ⇒(/ (* 12 (f (/ 12 3)) 2) 2) ⇒(/ (* 12 (- 4 1) 2) 2) ⇒(/ 72 2) ⇒36
|