Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstraction and Repetition

Similar presentations


Presentation on theme: "Abstraction and Repetition"— Presentation transcript:

1 Abstraction and Repetition
Procedures as code abstraction Defining functions in Scheme Recursion Recursion on lists Looping constructs CSE S. Tanimoto Procedural Abstraction and Repetition

2 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 S. Tanimoto Procedural Abstraction and Repetition

3 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 S. Tanimoto Procedural Abstraction and Repetition

4 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 S. Tanimoto Procedural Abstraction and Repetition

5 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 S. Tanimoto Procedural Abstraction and Repetition

6 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) CSE S. Tanimoto Procedural Abstraction and Repetition

7 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 S. Tanimoto Procedural Abstraction and Repetition

8 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 S. Tanimoto Procedural Abstraction and Repetition

9 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 S. Tanimoto Procedural Abstraction and Repetition

10 One-Way Recursion Doesn’t Do Sublists
> (censor '(This bad list (has a bad sublist))) (THIS LIST (HAS A BAD SUBLIST)) CSE S. Tanimoto Procedural Abstraction and Repetition

11 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 S. Tanimoto Procedural Abstraction and Repetition

12 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 S. Tanimoto Procedural Abstraction and Repetition

13 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 S. Tanimoto Procedural Abstraction and Repetition

14 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 S. Tanimoto Procedural Abstraction and Repetition

15 Functional Programming
Pure functional programming is programming using only pure functions. No assignment statements, no loops. CSE S. Tanimoto Procedural Abstraction and Repetition


Download ppt "Abstraction and Repetition"

Similar presentations


Ads by Google