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

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

View Full Document Right Arrow Icon
Recursive Scheme Functions: Abs-List (abs-list ’(1 -2 -3 4 0)) (1 2 3 4 0) (abs-list ’()) () (define (abs-list lst) ) 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 7

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

View Full DocumentRight Arrow Icon
Equality Checking The eq? predicate doesn’t work for lists. Why not?
Background image of page 8
Image of page 9
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 Right Arrow Icon
Ask a homework question - tutors are online