Abstraction and Repetition Procedures as code abstraction Defining functions in Scheme Recursion Recursion on lists Looping constructs CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition
CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition Code Abstraction Any programming language should provide ways to specify a lot of computation with a little code. Execution sequences that contain a lot of similar subcomputations should be represented using abstractions: An abstraction is a representation of what two or more things have in common. E.g. Two or more iterations of a loop have the sequence of instructions in the loop body in common. E.g., Two square-root computations have the square-root algorithm in common. CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition
Defining Procedures with DEFINE Procedures are the primary abstraction mechanism available in Scheme. (Others are structures and macros -- as well as iteration constructs). Non-built-in procedures are usually defined using the special form DEFINE. > (define (sqr x) (* x x)) > (sqr 9) 81 > (sqr 1.5) 2.25 CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition
Procedures with no arguments A procedure can take zero, one, or more arguments. > (define (say-hello) ; prints a greeting. (display "Hello there!") (newline) ) > (say-hello) Hello there! > CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition
COND (an alternative to IF) The COND form uses of a kind of abstraction from nested IF forms. > (define (small-prime? n) ; Returns T if n is a small prime. (cond ((= n 2) #t) ((= n 3) #t) ((= n 5) #t) ((= n 7) #t) (#t #f) ) ) > (small-prime? 9) #f > (small-prime? 3) #t CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition
CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition Recursive Procedures A procedure may be defined in terms of itself. > (define (factorial n) ; Returns factorial of N. (if (= n 1) 1 (* n (factorial (- n 1))) ) ) > (factorial 5) 120 > (factorial 20) 2432902008176640000 CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition
Primitive List-Manipulating Functions > (define x '(a b c d)) > (car x) ; CAR returns 1st elt of list A > (cdr x) ; CDR returns all but 1st. (B C D) > x (A B C D) ; X has not been changed. > (cons 'a 'b) (A . B) ; CONS combines two things. > (cons 'a ()) (A) ; The result is often a list. > (cons 'z x) (Z A B C D) CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition
CAR, CDR, and their combinations > (set! x '(a b c d)) (A B C D) > (cdr (cdr x)) (C D) > (cddr x) > (caddr x) C ; CADDR selects the 3rd elt. CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition
Recursive Functions of Lists > (define (censor lst) ; Returns LST with no instances of BAD. (cond ((null? lst) ()) ((eq? (car lst) 'BAD) (censor (cdr lst)) ) (#t (cons (car lst) (censor (cdr lst)) ) ) ) ) CENSOR > (censor '(This is a bad bad list)) (THIS IS A LIST) CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition
One-Way Recursion Doesn’t Do Sublists > (censor '(This bad list (has a bad sublist))) (THIS LIST (HAS A BAD SUBLIST)) CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition
Two-Way Recursive Functions > (define (censor2 lst) ; Returns LST with no instances of BAD. (cond ((null? lst) ()) ((not (list? Lst)) lst) ((eq? (car lst) 'BAD) (censor2 (cdr lst)) ) (#t (cons (censor2 (car lst)) (censor2 (cdr lst)) ) ) ) ) CENSOR2 > (censor2 '(This bad list (has a bad sublist))) (THIS LIST (HAS A SUBLIST)) CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition
CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition Looping: DO > (define (promise n) ; Prints a promise N times. (let ((the-promise "I will balance parentheses")) (do ((i 0 (+ i 1))) ((= i n)) (display i) (display ". ") (display the-promise) (newline) ) ) ) > (promise 3) 0. I will balance parentheses. 1. I will balance parentheses. 2. I will balance parentheses. CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition
Looping: DO (2nd example) > (define (print-on-separate-lines lst) ; Prints LST with one line per element. (do ((tmp lst (cdr tmp))) ((null? tmp)) (let ((elt (car tmp))) (display elt) (newline) ) ) ) > (print-on-separate-lines '(lunch around the corner) ) LUNCH AROUND THE CORNER CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition
CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition Pure Functions A pure function is one whose returned value depends only on its arguments (i.e., it possesses referential transparency), and it does not have any side effects. (define (plus3 x)(+ x 3)) ; a pure function (define (plus3-with-y x) ; not a pure function (set! y 3) (+ x y) ) (define (plus3-with-z x) ; not pure unless (+ x z) ) ; z is constant CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition
Functional Programming Pure functional programming is programming using only pure functions. No assignment statements, no loops. CSE 341 -- S. Tanimoto Procedural Abstraction and Repetition