Abstraction and Repetition Procedures as code abstraction Defining
Abstraction and Repetition • • • Procedures as code abstraction Defining functions in Scheme Recursion on lists Looping constructs CSE 341 -- S. Tanimoto Procedural Abstraction and 1
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 2
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 3
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 4
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 5
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 6
Primitive List-Manipulating Functions > (define x '(a > (car x) ; A > (cdr x) ; (B C D) > x (A B C D) ; > (cons 'a 'b) (A. B) ; > (cons 'a ()) (A) ; > (cons 'z x) (Z A B C D) b c d)) CAR returns 1 st elt of list CDR returns all but 1 st. X has not been changed. CONS combines two things. The result is often a list. CSE 341 -- S. Tanimoto Procedural Abstraction and 7
CAR, CDR, and their combinations > (set! x '(a b c d)) (A B C D) > (cdr x)) (C D) > (cddr x) (C D) > (caddr x) C ; CADDR selects the 3 rd elt. CSE 341 -- S. Tanimoto Procedural Abstraction and 8
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 list)) (THIS IS A LIST) CSE 341 -- S. Tanimoto Procedural Abstraction and 9
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 10
Two-Way Recursive Functions > (define (censor 2 lst) ; Returns LST with no instances of BAD. (cond ((null? lst) ()) ((not (list? Lst)) lst) ((eq? (car lst) 'BAD) (censor 2 (cdr lst)) ) (#t (cons (censor 2 (car lst)) (censor 2 (cdr lst)) ) ) CENSOR 2 > (censor 2 '(This bad list (has a bad sublist))) (THIS LIST (HAS A SUBLIST)) CSE 341 -- S. Tanimoto Procedural Abstraction and 11
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 12
Looping: DO (2 nd 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 13
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 (plus 3 x)(+ x 3)) ; a pure function (define (plus 3 -with-y x) (set! y 3) (+ x y) ) ; not a pure function (define (plus 3 -with-z x) (+ x z) ) ; not pure unless ; z is constant CSE 341 -- S. Tanimoto Procedural Abstraction and 14
Functional Programming Pure functional programming is programming using only pure functions. No assignment statements, no loops. CSE 341 -- S. Tanimoto Procedural Abstraction and 15
- Slides: 15