scheme_part_2

# scheme_part_2 - Recursive Scheme Functions: Abs-List...

This preview shows pages 1–9. Sign up to view the full content.

Recursive Scheme Functions: Abs-List (abs-list ’(1 -2 -3 4 0)) (1 2 3 4 0) (abs-list ’()) () (define (abs-list lst) ) 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Recursive Scheme Functions: Append (append ’(1 2) ’(3 4 5)) (1 2 3 4 5) (append ’(1 2) ’(3 (4) 5)) (1 2 3 (4) 5) (append ’() ’(1 4 5)) (1 4 5) (append ’(1 4 5) ’()) (1 4 5) (append ’() ’()) () (define (append x y) ) Note: There is a built-in append function. 2
Recursive Scheme Functions: Counting (define (atomcount x) (cond ((null? x) 0) ((atom? x) 1) (else (+ (atomcount (car x)) (atomcount (cdr x)))))) (atomcount ’(1 2)) 2 (atomcount ’(1 (2 (3)) (5))) 4 : (at ’(1 (2 (3)) (5))) (+ (at 1) (at ((2 (3)) (5)))) (+ 1 (+ (at (2 (3))) (at ((5))))) (+ 1 (+ (+ (at 2) (at ((3)))) (+ (at (5)) (at ())))) (+ 1 (+ (+ 1 (+ (at (3)) (at ()))) (+ (+ (at 5) (at ())) 0))) (+ 1 (+ (+ 1 (+ (+ (at 3) (at ())) 0)) (+ (+ 1 0) 0))) (+ 1 (+ (+ 1 (+ (+ 1 0) 0)) (+ 1 0))) (+ 1 (+ (+ 1 (+ 1 0)) 1)) (+ 1 (+ (+ 1 1) 1)) (+ 1 (+ 2 1)) (+ 1 3) 4 This is called “car-cdr-recursion.’ 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Efciency Issues Problem : Evaluating the same expression twice. Example: (define (longest-nonzero x y) (cond ((and (null? x) (null? y)) -1) ((> (length x) (length y)) (length x)) (else (length y)) )) What can you do if there is no assignment statement? 4
Efciency Issues Solution 1 : Bind expression values to parameters in a helper function. (define (maximum x y) (cond ((> x y) x) (else y) )) (define (longest-nonzero x y) (cond ((and (null? x) (null? y)) -1) (else (maximum (length x) (length y))) )) Note: There is a built-in max function. 5

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Efciency Issues Solution 2 : Use a let or let* construct, that binds variables to expression results. (let ((var1 expr1) ... (varn exprn)) <vars are defined and can be used here>) (let* ((var1 expr1) ... (varn exprn)) <vars are defined and can be used here>) Note : Do not use this in your Scheme assignment. 6
Polymorphic and Monomorphic Functions Polymorphic functions can be applied to arguments of many forms The function length is polymorphic: it works on lists of numbers, lists of symbols, lists of lists, lists of anything The function square is monomorphic: it only works on numbers 7

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Equality Checking The eq? predicate doesn’t work for lists. Why not?
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/05/2011 for the course CS 24 taught by Professor Cathy during the Summer '10 term at University of Toronto- Toronto.

### Page1 / 31

scheme_part_2 - Recursive Scheme Functions: Abs-List...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online