118
Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting Design Prof. Dr. Max Mühlhäuser Dr. Guido Rößling

Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Embed Size (px)

Citation preview

Page 1: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Telecooperation/RBG

Technische Universität Darmstadt

Copyrighted material; for TUD student use only

Introduction to Computer Science ITopic 5: Abstracting Design

Prof. Dr. Max MühlhäuserDr. Guido Rößling

Page 2: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

2

Outline

• Similarities in definitions• Functions are values• Designing abstractions from examples• Designing abstractions with functions as

values• Defining functions on the fly• “pipes-and-filters” organization of

computations

Page 3: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

3

Similarities in definitions

• Many data and function definitions look alike:– data: a list of symbols, a list of numbers, … – functions: searching for a specific symbol in a list of

symbols, searching for a specific number in a list of numbers …

• Repetitions are the source of programming mistakes– Eliminating them is the most important step in the

(program) editing process

• This part of the lecture is about:– Identifying similarities in function and data definitions – Design recipes to build abstractions to avoid them – The role of higher-order procedures in the abstraction

process

Page 4: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

4

Similarities in functions

• The use of our current design recipes determines a function's template (or basic organization) from the data definition for the input – The template is an alternative method of

expressing what we know about the input data.

• Consequence: functions that use the same kind of data look similar

Get your data structures correct first, and the rest of the program will write itself.

David Jones

Page 5: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

5

Two similar functions

;; contains-doll? : los  ->  boolean ;; to determine whether alos contains ;; the symbol 'doll

(define ( contains-doll? alos) (cond [(empty? alos) false] [else (cond [(symbol=? (first alos) 'doll) true] [else ( contains-doll? (rest alos ))])]))

Page 6: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

6

Two similar functions

;; contains-car? : los  ->  boolean ;; to determine whether alos contains ;; the symbol 'car

(define ( contains-car? alos) (cond [(empty? alos) false] [else (cond [(symbol=? (first alos) 'car ) true] [else ( contains-car? (rest alos))])]))

Page 7: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

7

A more general/abstract function• The more general function below requires an

additional parameter: the symbol that we are looking for– It is otherwise like the two original functions

;; contains? : symbol los -> boolean;; to determine whether alos contains the symbol s

(define (contains? s alos) (cond [(empty? alos) false] [else (cond

[(symbol=? (first alos) s) true] [else

(contains? s (rest alos))])]))

Page 8: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

8

Functional abstraction

• Defining abstract versions of functions is highly beneficial– A single function can perform many different tasks:

• contains? can search for many different symbols instead of just one concrete symbol

– Defining the single version solves many related problems at once

• The process of combining two related functions into a single definition is called functional abstraction

Page 9: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

9

Two other similar functions

;; less-than: lon number -> lon ;; constructs a list of those numbers ;; in alon that are less than t

(define (less-than alon t) (cond [(empty? alon) empty] [else (cond [(< (first alon) t) (cons (first alon) (less-than(rest alon) t))] [else (less-than(rest alon) t)])]))

less-than

Page 10: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

10

Two other similar functions

;; greater-than: lon number -> lon;; constructs a list of those numbers ;; in alon that are greater than t

(define (above alon t) (cond [(empty? alon) empty] [else (cond [(> (first alon) t) (cons (first alon) (greater-than (rest alon) t))] [else (greater-than (rest alon) t)])]))

greater-than

Page 11: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

11

Abstracting over the two functions

(define (filter1 rel-op alon t) (cond [(empty? alon) empty] [else (cond [(rel-op (first alon) t) (cons (first alon) (filter1 rel-op (rest alon) t))] [else (filter1 rel-op (rest alon) t)])]))

• The additional parameter, rel-op, of filter1 stands for both concrete relational operators in less-than and greater-than:

Page 12: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

12

Original functions from abstraction

;; less-than1 : lon number  ->  lon (define (less-than1 alon t) (filter1 < alon t))

;; greater-than1: lon number  ->  lon (define (greater-than1 alon t) (filter1 > alon t))

• To apply filter1 we must supply 3 arguments: – a relational operator R that compares two numbers, – a list L of numbers, a number N

• filter1 extracts all those items i in L for which (R i N) evaluates to true

• We can now define our functions less-than and greater-than as one-liners using filter1 – less-than1, resp. greater-than1, produce the same

results as less-than, resp. greater-than, on the same inputs

Page 13: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

13

Reusing abstract functions

• We can put an abstract function to other uses:– The first argument of filter1 can be any function that

uses two numbers and produces a boolean• Examples:

– (filter1 = alon t): extracts all those numbers in alon that are equal to t.

– (filter1 <= alon t): produces the list of numbers in alon that are smaller than or equal to t.

– (filter1 >= alon t): computes the list of numbers >= t.

– the following expression extracts those numbers in (list 1 2 3 4 5) with a square value larger than 10.

;; squared>? : number number  ->  boolean (define (squared>? x c) (> (* x x) c))

(filter1 squared>? (list 1 2 3 4 5) 10)

Page 14: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

14

Similarities in Data Definitions

• Given the similarity between the data definitions, functions that use elements of these classes are similar, too.

A list-of-numbers is either• empty • (cons n l)

• n is a number• l is a list-of-numbers

A list-of-IRs is either• empty • (cons n l)

• n is an IR • l is a list-of-IRs

(define-struct ir (name price))

An IR (Inventory Record) is a structure: (make-ir n p) 

where n is a symbol and p is a number

Page 15: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

15

Similarities in data definitions

;; less-than: number lon  ->  lon ;; to construct a list of those numbers ;; on alon that are less than t

(define (less-than alon t) (cond [(empty? alon) empty] [else (cond [(< (first alon) t)

(cons (first alon) (less-than (rest alon) t))]

[else (less-than (rest alon) t)])]))

Page 16: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

16

Similarities in data definitions

;; less-than-ir : number loIR  ->  loIR ;; to construct a list of those records ;; on aloir that contain a price less than t

(define (less-than-ir aloir t) (cond [(empty? aloir) empty] [else (cond

[(<ir (first aloir) t)

(cons (first aloir) (less-than-ir (rest aloir) t))] [else (less-than-ir (rest aloir) t)])]))

;; <ir : IR number  ->  boolean

(define (<ir ir p) (< (ir-price ir) p))

Page 17: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

17

Polymorphic functions

• If we abstract over less-than and less-than-ir, we obtain again filter1. – Hence, we can define less-than-ir in terms of filter1: (define (less-than-ir1 aloir t)

(filter1 <ir aloir t))

• The abstract function filter1 can filter not only lists of numbers but also lists of arbitrary things:– As long as we can define a function that compares

these arbitrary things with numbers … e.g., <ir

– More abstract: … as long as we can define a function that compares list of items with items passed to filter1 as the second argument …

Page 18: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

18

Polymorphic functions

;; find : loIR symbol  ->  boolean ;; determines whether aloir contains a record for t (define (find aloir t) (cons? (filter1 eq-ir? aloir t)))

;; eq-ir? : IR symbol  ->  boolean ;; to compare ir's name and p (define (eq-ir? ir p) (symbol=? (ir-name ir) p))

• filter1 uniformly works on many shapes of input data:– filter1 applied to a list of X returns a list of

X - no matter what kind of Scheme data X is

• Polymorphic or generic function

Page 19: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

19

Parametric data definitions• Question: How do we write precise contracts for

polymorphic functions such as filter1?• Answer: parametric data definitions

– Do not specify everything about a class of data – Use variables to say that any form of Scheme data can

be used in a certain place

• An arbitrary collection of Scheme data

• Replace ITEM with symbol, number, IR, etc., to get a concrete instance of this abstract data definition:– lists of symbols, list of numbers,

list of IRs, etc.

A list of ITEM is either • empty or • (cons s l)

• s is an ITEM • l is a list of ITEM. 

TYPE VARIABLE

Page 20: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

20

Contracts for polymorphic functions

• In defining contracts, the abbreviation (listof X) states that a function works on all lists:

;; length : (listof X)  ->  number ;; to compute the length of a list (define (length alox) (cond [(empty? alox) 0] [else (+ (length (rest alox)) 1)]))

• X is just a variable a name that stands for some class of data

• If we apply length to an element of, say, (listof symbol) or (listof IR), we get a number

Page 21: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

21

Outline

• Similarities in definitions• Functions are values• Designing abstractions from examples• Designing abstractions with functions as

values• Defining functions on the fly• “pipes-and-filters” organization of

computations

Page 22: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

22

The value of procedures

• We have already discussed the value of procedures:– Isolate the implementation of a concept

• “separation of concerns” instead of code repetition– Code Reuse: locality of changes etc.– Changes at one place only, etc.– Allows recursion– Unit of information hiding

• The procedures that we have been using so far are called first-order procedures

• In this slide set, we have used a new, more powerful kind of procedures called higher-order procedures– Higher-order procedures take other procedures as

parameters or return procedures as their result

Page 23: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

23

Procedures as general methods

• First-order procedures: make procedures independent of particular data involved

• Higher-order procedures: express general methods of computation, independent of the particular functions involved

• Let us take a more systematic look at higher-order procedures– We will need to first adopt Scheme’s basic definition

• Higher-order functions violate the basic language definition

• From now on, you should use the “Intermediate Student with Lambda” language level in DrScheme

– Then we continue to discuss contracts for polymorphic functions

Page 24: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

24

Two violations of the basic language definition

Violation 1: (filter1 < (cons 4 empty) 5)

(define (find aloir t) (cons? (filter1 eq-ir? aloir t)))

<var><fct>

== x | alon | ... area-of-disk | perimeter | ...

Function names as arguments in applications Reason: An argument is an expression

and the class of expressions does not

include function names

<exp> =   <var>

| <con>

| (<prm> <exp> ...<exp>)

| (<fct> <exp> ...<exp>)

| (cond (<exp> <exp>) ...(<exp> <exp>))

| (cond (<exp> <exp>) ...(else <exp>)) | (local ...)

Page 25: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

25

Two violations of the basic language definition

Violation 2:Violation 2:Parameters are used in the first position of applications (as if they were functions).

Reason: Our grammar allows only names of functions (<fct>) and primitive operations (<prm>) in this place, but not parameters

(define (filter1 rel-op alon t) (cond [(empty? alon) empty] [else (cond [(rel-op (first alon) t) (cons (first alon) (filter1 rel-op (rest alon) t))] [else (filter1 rel-op (rest alon) t)])]))

Page 26: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

26

Adjusting basic Scheme

1. Include the names of functions and primitive operations in the definition of <exp>

2. Allow the first position in an application to be things other than function names and primitive operations:– At least variables that correspond to function

parameters – more generally: any expression<exp> =   <var>

| <con> | <prm> | <fct>

| (<exp> <exp> ...<exp>)

Page 27: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

27

Adjusting basic Scheme

• The accommodation of higher-order functions does not lengthen the grammar; it rather makes it simpler!

<exp> =   <var> | <con>

| <prm> | <fct>

| (<exp> <exp> ...<exp>)

<exp> =   <var>

| <con>

| (<prm> <exp> ...<exp>) | (<fct> <exp> ...<exp>)

Page 28: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

28

Adjusting basic Scheme

• The evaluation rules do not change at all– What changes is the set of values: it

includes the names of functions and primitive operations

<val> =   <boo> | <sym> | <num> | empty | <lst>

| <var> (names of defined functions)

| <prm> | <fct>

<lst> = empty | (cons <val> <lst>)

<val> = <con>

Page 29: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

30

Contracts for abstractand polymorphic functions

• What is the problem?– Our new functions use a type of values that we never

before used as data: • Primitive operations and other functions

– To write contracts for functions of functions, we need a way to describe the new class of values: primitive operations and other functions

• That is why we have so far postponed writing contracts for these functions– This is what we will consider in the next few slides

Page 30: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

31

Contracts for abstractand polymorphic functions

• But, we do have contracts for first-order functions, e.g.,

;; rel-op : number number  ->  boolean

• We say that “” describes a class of values: functions – Names on the left of  ””  specify what each value in the

class of functions must be applied to – The name on the right of  ”” states what each value in

the class of functions is going to produce, if applied to proper values

• In general: (A B  C) denotes the class of functions that take an element in A and an element in B and produce an element in C – Functions “from A and B to C”

Page 31: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

32

Example contract definitionsFirst version of

filter1;; filter1 : (number number -> boolean) lon number -> lon ;; to construct the list of those numbers n on alon for which ;; (rel-op n t) evaluates to true

(define (filter1 rel-op alon t) ...)

More abstract version of filter1filter1 : (X number -> boolean) (listof X) number -> (listof X)

X stands for an arbitrary collection of Scheme data. We can replace it with anything, as long as all three occurrences are replaced by the same thing.

Page 32: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

33

Applying functions of functions

Does this application make sense?

filter1 : (X number -> boolean) (listof X) number -> (listof X)

(number number -> boolean)

listof number

(filter1 < (list 3 8 10) 2 )

The two classes – listof number and (number number -> boolean) - are identical to the first two argument parts of filter1's contract if X is replaced by number

Generally: to ensure that arguments make sense, we must find replacements of the variables in contracts so that the contract and the classes of the arguments match.

Page 33: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

34

A new use for filter1• Using filter1 to extract all toys with the same name from

a list of inventory records:

;; find : (listof IR) symbol -> (listof IR) (define (find aloir t) (filter1 eq-ir? aloir t))

;; eq-ir? : IR symbol -> boolean (define (eq-ir? ir s) (symbol=? (ir-name ir) s))

• Problem: “Threshold” argument in this use of filter1 is a symbol, not a number conflict with the contract of filter1

• To overcome the problem, we make the contract more abstract by introducing a new variable, TH for thresholds, that stands for an arbitrary collection of data:

filter1 : (X TH -> boolean) (listof X) TH -> (listof X)

filter1 : (X number -> boolean) (listof X) number -> (listof X)

Page 34: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

35

Summary: Contracts and Types

• Function contracts are made up of TYPEs: – basic types, e.g., number, symbol, boolean, empty– defined types, e.g., inventory-record, list-of-numbers, family-tree

– function types, e.g., (number -> number)– parametric types: either a defined types or function

types with type variables • To use a function with a parametric type:

– Find a replacement for all variables in the contract of the function so that the arguments belong to proper classes

– If this cannot be done: • Either revise the contract • Or question the decision to reuse the function

Page 35: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

36

Outline

• Similarities in definitions• Functions are values• Designing abstractions from examples• Designing abstractions with functions as

values• Defining functions on the fly• “pipes-and-filters” organization of

computations

Page 36: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

37

Recipe for abstracting from examples

• We have already abstracted designs from two concrete function definitions: – compare examples, – mark differences, – abstract

• Now we formulate these steps as a recipe: 1. Compare the examples and mark the differences by

boxes2. Abstract if boxes contain values3. Test the validity of the abstraction4. Formulate the contract of the abstraction

Page 37: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

38

Comparing examples

When we find two function definitions that are almost the same except at a few places and for their names:• We compare them and mark the differences with boxes• If the boxes contain only values, we can abstract

Each box contains a functional value, so we can abstract …

;; names : loIR -> los (define (names aloIR) (cond [(empty? aloIR) empty] [else (cons (IR-name (first aloIR)) (names (rest aloIR)))]))

;; convertCF : lon -> lon (define (convertCF alon) (cond [(empty? alon) empty] [else (cons (C->F (first alon)) (convertCF (rest alon)))]))

Page 38: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

39

Two steps of the abstraction

1. Replace the contents of corresponding pairs of boxes with new names and add these names to the parameter list. – We need as many names as there are pairs of

boxes

(define (names f aloIR) (cond [(empty? aloIR) empty] [else (cons (f (first aloIR)) (names (rest aloIR)))]))

(define (convertCF f alon) (cond [(empty? alon) empty] [else (cons (f (first alon)) (convertCF (rest alon)))]))

Page 39: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

40

Two steps of the abstraction2. The two definitions must now be the same,

except for the function names – To obtain the abstraction, systematically replace

the function names with one new name

(define (map f lox) (cond [(empty? lox) empty] [else (cons (f (first lox)) (map f (rest lox)))]))

map captures a common pattern for operations over lists it abstracts over the operation to be performed on the elements of the list

Page 40: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

41

Testing the abstraction

• Goal: Validate that the new function is a correct abstraction of the original concrete functions.

• Approach: – Define original functions in terms of the abstract one – Test the new versions with the examples formulated

in the process of defining the original functions

Page 41: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

42

Defining the original functionsin terms of the abstraction

• Assumptions:– The abstract function is called f-abstract, – One original function is called f-original and uses one

argument • If f-original differs from the other concrete

function in the use of one value, say, boxed-value, then we define the following function:

• For every proper value V, the following should hold:

(define (f-from-abstract x) (f-abstract boxed-value x))

(f-from-abstract V) = (f-original V)

Page 42: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

43

Defining the original functionsin terms of the abstraction

To ensure that these two definitions are equivalent to the old ones, i.e., that map is a correct abstraction, apply these two functions to examples specified for the development of convertCF and names

;; convertCF-from-map : lon -> lon (define (convertCF-from-map alon) (map C->F alon))

;; names-from-map : loIR -> los (define (names-from-map aloIR) (map IR-name aloIR))

Page 43: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

44

Formulating the contract

• Goal: Formulate a contract for the abstract function

• Note: In general, one cannot formulate the contract of the abstract function by looking at it as an abstraction of one or the other original functions

If we view map as an abstraction of names, we get:

(number -> number) (listof number) -> (listof number)

If we view map as an abstraction of convertCF, we get:

(IR -> symbol) (listof IR) -> (listof symbol)

The first contract would be useless in the second case, and vice versa

Page 44: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

45

Formulating general contracts

• By looking at the definition, we can see that map applies its first argument - a function f - to every item on the second argument - a list l

• This implies: – f must use the data that l contains – Hence, f has the contract X -> ??? if l contains values

of class X • If f produces Ys, map produces a list of Ys:

map : (X -> Y) (listof X) -> (listof Y)

Page 45: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

46

Contracts and new uses of abstract functions

• An abstract function is generally useful in a much broader context than we first anticipate: – For example, map can be used whenever we need to

produce a new list by processing all items on an existing list

;; list-of-squares : (listof numbers) -> (listof numbers) ;; constructs the list of squares of the elements of alon (define (list-of-squares alon) (cond [(empty? alon) empty] [else (cons (sqr (first alon)) (list-of-squares (rest alon)))]))

(define (list-of-squares list) (map sqr list))

(list-of-squares (list 1 2 3 4

5))--> (list 1 4 9 16 25)

Page 46: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

47

Contracts and new uses of abstract functions

• Question: How do we discover new uses of abstract functions? – We have no recipe to guide the discovery process– It is a matter of practice to develop an eye for

matching abstract functions to situations

• The formulation of the contract is important to increase the usefulness of an abstract function:– Formulate contracts that describe the applicability of

abstract functions in the most general terms possible

Page 47: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

48

Formulating general contracts

• Abstracting contracts follows the same recipe as for abstracting functions:– Compare contracts of examples from which we create

abstractions – Replace specific classes in corresponding positions,

one at a time, to make the contract more general– Check that the contract properly describes the specific

instances of the abstracted function  

(number -> number) (listof number) -> (listof number)

(IR -> symbol) (listof IR) -> (listof symbol)

(X -> Y) (listof X) -> (listof Y)

X Y

Page 48: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

49

[Recall … ]

• First-order procedures: make procedures independent of particular data (numbers, symbols, …) involved

• Higher-order procedures: express general methods of computation, independent of the particular functions involved

Let us examine what that means by the example of map…

Page 49: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

50

Higher-order functions as abstraction barriers

by the example of map(define (list-of-squares alon) (cond [(empty? alon) empty] [else (cons

(sqr (first alon)) (list-of-squares (rest alon)))]))

• The above version of list-of-squares is sub-optimal because of two reasons:• It draws attention to the element-by-element

processing of lists• It reveals too much details about the

implementation of lists: how list elements are extracted and combined

• The operation to apply to each element is hard-coded

Page 50: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

51

Higher-order functions as abstraction barriers

by the example of map(define (list-of-squares list) (map sqr list))

• The map version of list-of-squares suppresses this level of detail – It emphasizes that squaring is a

transformation of a list of elements to another list of results

– Higher level of abstraction in dealing with lists

• The computer is performing the same process• The difference is that we think differently about

the process!

Page 51: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

52

Higher-order functions as abstraction barriers

by the example of map• Abstraction barrier: – map isolates the implementation of procedures that

transform lists from the details of how the elements of the list are extracted and combined

low-level details of how sequences are implemented

MAP

operations that transform sequences to sequences

• We can vary the implementation of the list independent of the mapping function applied to each element

Page 52: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

54

Abstracting from templates

• When designing functions that use the same kind of data, we reuse the same template – Function definitions look similar we abstract them– We could abstract directly from the templates

(define (fun-for-l l) (cond [(empty? l) ...] [else ... (first l) ... (fun-for-l (rest l)) ...]))

A list-of-numbers is either• empty • or (cons n l) where

• n is a number• l is a list-of-numbers

• To derive a concrete list-processing function f, we fill two gaps:– In the first clause, we typically place a plain value – For the second clause, we combine (first l) and (f (rest l))

Page 53: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

55

Abstracting from templates: fold

;; fold : Y (X Y -> Y) (listof X) -> Y (define (fold init combine-op lst) (cond [(empty? lst) init] [else (combine-op (first lst) (fold init combine-op (rest lst)))]))

;; sum : (listof number) -> number (define (sum lst) (fold 0 + lst))

;; product : (listof number) -> number (define (product lst) (fold 1 * lst))

fold captures a common pattern for processing lists: applying a binary operation to the first element of the list and the result of folding the rest of the list

Page 54: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

56

[Note on Folding in Scheme]• Folding is such a common operation for lists that Scheme

provides it as a predefined operation• Note that there are two versions of fold in Scheme

– foldr folds from right to left– foldl folds from left to right

;; foldr : (X Y -> Y) Y (listof X) -> Y

;; (foldr f base (list x-1 ... x-n)) =

;; (f x-1 ... (f x-n base))

(define (foldr f base alox) ...)

;; foldl : (X Y -> Y) Y (listof X) -> Y

;; (foldl f base (list x-1 ... x-n)) =

;; (f x-n ... (f x-1 base))

(define (foldl f base alox) ...)

Page 55: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

57

Deriving sort from fold

Can we use fold to sort lists?

Recall insertion sort…

Page 56: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

58

[Recall insert]

eli,i = [1… j-1], eli <= an elj elj+1 ... eln

alon

an

an

Insert for non-empty alon:bypass alon elements until the first element, ej, that is larger than an. Insert an between elj-1 and elj

;; insert : number list-of-numbers -> list-of-numbers

(define (insert an alon)

(cond

[(empty? alon) (cons an empty)]

[(<= an (first alon)) (cons an alon)]

[else (cons (first alon) (insert an (rest alon)))]))

Page 57: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

59

[Recall: insertion sort];; sort : list-of-numbers  ->  list-of-numbers

;; creates a sorted list of numb. from numbers in alon

(define (sort alon)

(cond

[(empty? alon) empty]

[else (insert (first alon) (sort (rest alon)))]))

anan

sortedunsorted

Page 58: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

60

Deriving sort from fold

;; sort : (listof number) -> (listof number) (define (sort l) (local ((define (insert an alon) ... )

(fold empty insert l)))

;; fold : Y (X Y -> Y) (listof X) -> Y (define (fold init combine-op l) (cond [(empty? l) init] [else (combine-op (first l) (fold init combine-op (rest l)))]))

init empty

combine-op insert

Page 59: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

61

sort @ work

(sort l) = (fold empty insert (list 3 2 8))= (insert 3 (fold empty insert (list (2 8))))= (insert 3 (insert 2 (fold empty insert (list 8))))= (insert 3 (insert 2 (insert 8 (fold empty insert `()))))= (insert 3 (insert 2 (insert 8 `())))= (insert 3 (insert 2 (list 8)))= (insert 3 (list 2 8)= (2 3 8)

2

3 2 8

Page 60: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

62

Abstraction and single point of control

• Creating an abstraction often simplifies other definitions

• The process of abstracting may uncover problems with existing functions

• Abstracted function definitions are more flexible and more widely usable than specialized definitions

• But, the single most important advantage of abstraction is that it creates a single point of control for the functionality in a program. – It (as much as possible) puts the definitions related to

some specific task in one place– Separation of Concerns

Page 61: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

63

Why “single point of control”?

• Putting the definitions for a specific task in one place makes it easier to maintain a program

• Program maintenance means:– Fixing the program so that it functions properly in

previously untested cases– Extending the program so that it can deal with new or

unforeseen situations– Changing the representation of some information as

data (for example, calendar dates)

• There are estimations that maintenance makes up most of the lifetime of successful programs (60-80%)

Page 62: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

64

Why “single point of control”?

• We can change a single definition to fix and improve many different uses– An equally important advantage of abstracted

definitions

• Example: Two modifications of filter1 on the next two slides:– The first modification flattens the nested cond-

expression• Something that an experienced programmer may wish

to do – The second modification uses a local-expression to

make the nested cond-expression more readable

Page 63: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

65

Why “single point of control”?(define (filter1 rel-op alon t) (cond [(empty? alon) empty] [else (cond [(rel-op (first alon) t) (cons (first alon) (filter1 rel-op (rest alon) t))] [else (filter1 rel-op (rest alon) t)])]))

(define (filter1 rel-op alon t) (cond [(empty? alon) empty] [(rel-op (first alon) t) (cons (first alon)

(filter1 rel-op (rest alon) t))] [else (filter1 rel-op (rest alon) t)]))

Page 64: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

66

Why “single point of control”?

(define (filter1 rel-op alon t) (cond [(empty? alon) empty] [else (local ((define first-item (first alon)) (define rest-filtered

(filter1 rel-op (rest alon) t))) (cond [(rel-op first-item t) (cons first-item rest-filtered)] [else rest-filtered]))]))

(define (filter1 rel-op alon t) (cond [(empty? alon) empty] [else (cond [(rel-op (first alon) t) (cons (first alon) (filter1 rel-op (rest alon) t))] [else (filter1 rel-op (rest alon) t)])]))

Page 65: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

67

Why “single point of control”?

• All uses of filter1, including those to define the functions less-than1 and greater-than1, benefit from the changes

• Similarly, if the modification had fixed a logical mistake, all uses of the function would be improved

• Finally, it is even possible to add new tasks to abstracted functions, for example, a mechanism for counting how many elements are filtered – All uses of the function would benefit from the new

functionality

Page 66: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

68

Why “single point of control”?

• With everything in one place:– Understanding what to fix, extend, or change means

understanding one function– Fixing an error means fixing it in one function, not

multiple similar versions. – Extending the capabilities of a function means fixing

one function, not its related copies.– Changing a data representation means changing a

general data-traversal function, not all those that came from the same template.

Guideline on Creating Abstractions:Form an abstraction instead of copying and modifying a piece of a program.

Page 67: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

69

The role of abstraction

• Experience teaches us that maintaining software is expensive

• Programmers can reduce the maintenance cost by organizing programs correctly

• First principle: match the structure of the function to the structure of its input data– This rule makes it easy to modify and extend functions

when the set of possible input data changes• Second principle: introduce proper abstractions

– Every abstracted function creates a single point of control for at least two different functions, often for several more

Page 68: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

70

The role of abstraction

• The best programmers are those who actively edit their programs to build new abstractions so that they collect things related to a task at a single point.

• Our design recipe for abstracting functions is the most basic tool to create abstractions. – To use it requires practice. As we practice, we expand our

capabilities for building and using abstractions. • We use functional abstraction to study this

practice. – While not all languages provide the freedom to abstract

functions as easily as Scheme, modern languages often support similar concepts

– Practicing in powerful languages such as Scheme is the best possible preparation

Page 69: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

71

Outline

• Similarities in definitions• Functions are values• Designing abstractions from examples• Designing abstractions with functions as

values• Defining functions on the fly• “pipes-and-filters” organization of

computations

Page 70: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

72

Functions that produce functions

• Expressions in the “new” Scheme can evaluate to functions.

• Because the body of a function definition is also an expression, a function can produce a function.

• Functions that produce functions are useful especially if the produced function “remembers” values of the producing function’s arguments at application time

(define (f x) first)(define (g x) f) (define (h x) (cond ((empty? x) f) ((cons? x) g)))

(define (add-10 x) (+ x 10))(define (add-5 x) (+ x 5)) (define (h x) (cond [(< x 5) add-5] [else add-10])) (define addX (h 12))(addX 7) -> 17

Page 71: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

73

Creating functions with “memory”

• The most interesting property of add:– Its result “remembers” the value of the argument x – Each time we use the result of applying add, it uses

the value of x at application time of add

;; add : number -> (number -> number) ;; to create a function that adds x to its input (define (add x) (local ((define (x-adder y) (+ x y))) x-adder))

Functions with „memory“ are obtained by combining local-expressions and higher order functions

Page 72: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

74

Creating functions with “memory”

x5

4 95 2 7

88

2 101 9

(define f (add 5)) = (define f (local ((define (x-adder y) (+ 5 y))) x-adder)) = (define f (local ((define (x-adder5 y) (+ 5 y))) x-adder5)) = (define (x-adder5 y) (+ 5 y)) (define f x-adder5)

(f 10) = (x-adder5 10) = (+ 5 10) = 15

add (add 5)

(add 8)

Page 73: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

75

Abstracting with functions with memory

• This form of “memory” due to combining local-expressions and functions-as-values can be used to simplify our recipe for creating abstract functions

• Recall our recipe for abstracting from examples:– Compare and place boxes around differences – Replace the contents of the boxes with variables– Bind these variables by adding them to the

argument list

• Alternative recipe:– Wrap the definition in a local with the content in

boxes replaced by free variables– Prefix the local block with a function that uses the

new variables

Page 74: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

76

Abstracting with functions with memory

;; less-than: lon number  ->  lon ;; to construct a list of those numbers ;; on alon that are less than t

(define (less-than alon t) (cond [(empty? alon) empty] [else (cond [(< (first alon) t) (cons (first alon) (less-than (rest alon) t))] [else (less-than(rest alon) t)])]))

Page 75: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

77

Abstracting with functions with memory

;; greater-than : lon number  ->  lon;; to construct a list of those numbers ;; on alon that are greater than t

(define (greater-than alon t) (cond [(empty? alon) empty] [else (cond [(> (first alon) t) (cons (first alon) (greater-than (rest alon) t))] [else (greater-than(rest alon) t)])]))

Page 76: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

78

Abstracting with functions with memory(define (filter2 rel-op)

(local (

(define (abs-fun alon t) (cond [(empty? alon) empty] [else (cond [(rel-op (first alon) t) (cons (first alon) (abs-fun (rest alon) t))] [else (abs-fun (rest alon) t)])])))

abs-fun))

Like add, filter2 uses an argument, defines a function, and returns this function as a result. The result remembers the rel-op argument forever.

Page 77: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

79

Alternative abstraction recipe

The comparison: compare and mark differences The abstraction:

– Place one of the functions into a local-expression and use the name of the function as the body of the local:

– Create the abstract function by listing the names in the boxes as parameters:

– If op1 or op2 is a special symbol, e.g. <, name it something that is more meaningful in the new context

(local ((define (concrete-fun x y z) ... op1 ... op2 ...)) concrete-fun)

(define (abs-fun op1 op2) (local ((define (concrete-fun x y z) ... op1 ... op2 ...)) concrete-fun))

Page 78: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

80

Alternative abstraction recipe

• The test: Derive the concrete functions as before as instances of filter2. – less-than, greater-than as instances of filter2:

• The contract: – The contract of an abstract function defined with this

recipe contains two arrows.• The function produces a function• To describe this relationship ,the type to the right of the

first arrow must contain another arrow. – Example: the contract for filter2:

(define less-than2 (filter2 <)) (define greater-than2 (filter2 >))

;; filter2 : (X Y -> boolean) -> ((listof X) Y -> (listof X))

Page 79: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

81

Outline

• Similarities in definitions• Functions are values• Designing abstractions from examples• Designing abstractions with functions as

values• Defining functions on the fly• “pipes-and-filters” organization of

computations

Page 80: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

82

Motivating on the fly function definition

• Many uses of abstract functions require the definition of auxiliary functions – filter1 can be used with <ir, eq-ir, … <your function>;; find : (listof IR) symbol -> (listof IR)

(define (find aloir t) (filter1 eq-ir? aloir t))

;; eq-ir? : IR symbol -> boolean (define (eq-ir? ir s) (symbol=? (ir-name ir) s))

(define (filter1 rel-op alon t) (cond [(empty? alon) empty] [else (cond [(rel-op (first alon) t) (cons (first alon) (filter1 rel-op (rest alon) t))] [else (filter1 rel-op (rest alon) t)])]))

Recall the definition of filter1:

Page 81: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

83

Motivating on the fly function definition

• If auxiliary functions are only used as arguments to some abstract function f, we use a local-expression;; find : list-of-IRs symbol -> boolean (define (find aloir t) (local ((define (eq-ir? ir p) (symbol=? (ir-name ir) p))) (filter1 eq-ir? aloir t)))

;; find : list-of-IRs number -> boolean (define (find aloir t) (filter1 (local ((define (<ir ir p)(< (ir-price ir) p))) <ir) aloir t))

Page 82: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

84

Motivating on the fly function defs

• Because good programmers use abstract functions and organize their programs in a tidy manner, Scheme provides a short-hand for this particular, frequent use of local

• The short-hand is called a lambda-expression – facilitates the introduction of functions like eq-ir? or <ir

• Next slides:– Syntax and semantics of lambda-expression – Pragmatics

Page 83: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

85

Syntax of lambda• A lambda-expression is a new form of expression

distinguished by the keyword lambda

• A lambda-expression defines an anonymous function– sequence of variables behind the keyword lambda are the

function's parameters– third component is the function's body

• Examples

1.(lambda (x c) (> (* x x) c)) 2.(lambda (ir p) (< (ir-price ir) p)) 3.(lambda (ir p) (symbol=? (ir-name ir) p))

<exp> = (lambda (<var> ... <var>) <exp>)

(define (plus4 x) (+ x 4)) is equivalent to(define plus4 (lambda (x) (+ x 4)))

Page 84: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

86

Scope and Semantics of lambda

• (lambda (x-1 ... x-n) exp) introduces x-1 ... x-n as binding occurrences and specifies that the scope of parameters is exp

(lambda (x-1 ... x-n) exp)

(local ((define (a-new-name x-1 ... x-n) exp)) a-new-name)

a-new-name may not occur in exp

Page 85: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

87

Using lambda instead of local;; find : list-of-IRs number -> boolean (define (find aloir t) (filter1 (local ((define (<ir ir p) (< (ir-price ir) p))) <ir) aloir t))

;; find : list-of-IRs number -> boolean (define (find aloir t) (filter1 (lambda (ir p) (< (ir-price ir) p)) aloir t))

Page 86: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

88

lambda instead of local

;; add : number -> (number -> number) ;; to create a function that adds x to its input(define (add x) (local ((define (x-adder y) (+ x y))) x-adder))

;; add : number -> (number -> number) ;; to create a function that adds x to its input (define (add x) (lambda (y) (+ x y)))

Three steps: Create a procedure value Give it a local name Evaluate the name to get the

value, which is returnedWhy the indirection? Better: directly create and return the procedure value

Page 87: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

89

Using lambda instead of local

(define (filter2 rel-op) (local ( (define (abs-fun alon t) (cond [(empty? alon) empty] [else (cond [(rel-op (first alon) t) (cons (first alon) (abs-fun (rest alon) t))] [else (abs-fun (rest alon) t)])])) ) abs-fun))

Quiz: Can you replace local with lambda here?

Page 88: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

90

Scope and Semantics of lambda

• Basic facts that govern the evaluation of lambda-expressions: – A lambda-expression is a value because functions are

values.– The application of lambda-expressions to values

proceeds according to our usual laws of function application, assuming we expand the short-hand first

(lambda (x-1 ... x-n) exp)

(local ((define (a-new-name x-1 ... x-n) exp)) a-new-name)

Page 89: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

91

Semantics of lambda

• To illustrate, let us evaluate the following application:

(define (filter1 rel-op alon t) (cond [(empty? alon) empty] [else (cond [(rel-op (first alon) t) (cons (first alon) (filter1 rel-op (rest alon) t))] [else (filter1 rel-op (rest alon) t)])]))

Recall the definition of filter1:

(filter1 (lambda (ir p) (< (ir-price ir) p)) (list (make-ir 'doll 10)) 8)

Page 90: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

92

Semantics of lambda

(filter1 (lambda (ir p) (< (ir-price ir) p)) (list (make-ir 'doll 10)) 8) ...

= (filter1 (local ((define (<ir ir p) (< (ir-price ir) p))) <ir ) (list (make-ir 'doll 10)) 8)

= (filter1 <ir (list (make-ir 'doll 10)) 8)...

substitute lambda with local

Page 91: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

93

Semantics of lambda

• We can also explain the semantics of lambda directly:

((lambda (x-1 ... x-n) exp) val-1 ... val-n)

= exp with all occurrences of x-1 ... x-n replaced by val-1 ... val-n

Page 92: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

94

Semantics of lambda

• To illustrate, let’s evaluate the application again

(define (filter1 rel-op alon t) (cond [(empty? alon) empty] [else (cond [(rel-op (first alon) t) (cons (first alon) (filter1 rel-op (rest alon) t))] [else (filter1 rel-op (rest alon) t)])]))

Recall the definition of filter1:

(filter1 (lambda (ir p) (< (ir-price ir) p)) (list (make-ir 'doll 10)) 8)

Page 93: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

95

Semantics of lambda (filter1 (lambda (ir p) (< (ir-price ir) p)) (list (make-ir 'doll 10)) 8) = (cond [((lambda (ir p) (< (ir-price ir) p)) (make-ir 'doll 10) 8) (cons (first (list (make-ir 'doll 10))) (filter1 (lambda (ir p) (< (ir-price ir) p)) (rest (list (make-ir 'doll 10))) 8))] [else (filter1 (lambda (ir p) (< (ir-price ir) p)) (rest (list (make-ir 'doll 10))) 8) ]) = (cond [(< (ir-price (make-ir 'doll 10)) 8) (cons (first (list (make-ir 'doll 10))) (filter1 (lambda (ir p) (< (ir-price ir) p)) (rest (list (make-ir 'doll 10))) 8))] [else (filter1 (lambda (ir p) (< (ir-price ir) p)) (rest (list (make-ir 'doll 10))) 8)]) = ...

1st el. of list

Page 94: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

96

Pragmatics of lambda

Guideline on Lambda Expressions Use lambda-expressions when a function is not recursive and is only needed once as an argument

Page 95: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

97

Why “Lambda”?• The word “Lambda” stems from -Calculus

– A mathematical calculus defined by Alonso Church– More or less the core of Scheme, a minimal but

complete language:• It has only lambda abstraction and procedure application• no primitive types and/or procedures, • no special forms, etc.

– Basic tool for mathematical investigation and formal treatment of programming languages

• For now, you can simply think of lambda as make-procedure– Yet, let us take a look at some lambda brain twisters

using higher-order procedures

Page 96: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

98

Lambda the Ultimate…

• Can I compute without primitive values/types, special forms!?

• Yes! Represent everything as a function …e.g., the primitive operators and/or

(define my-and (lambda (op1 op2) (cond [op1 (cond [op2 true] [else false])] [else false])))

(define my-or (lambda (op1 op2) (cond [op1 true] [else (cond [op2 true] [else false])])))

Page 97: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

99

Lambda the Ultimate…

• Special forms cannot be passed as parameters …

• For illustration, consider Scheme functions andmap and ormap

Fine, but why do I want to represent booleans, and the and/or operations as

functions?

Because this way they become composable recall closure principle!

Page 98: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

100

Lambda the Ultimate…

… the case of andmap and ormap

(andmap even? (list 2 6)) = true

;; andmap : (X -> boolean) (listof X) -> boolean;; determines whether p holds for every item on alox;; (andmap p (list x-1 ... x-n)) = ;; (and (p x-1) (and ... (p x-n)))

(define (andmap p alox) (cond [(empty? alox) true] [else (and (p (first alox))

(andmap p (rest alox)))]))

Page 99: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

101

Lambda the Ultimate…… the case of andmap and ormap

(ormap even? (list 5 1) = false

;; ormap : (X -> boolean) (listof X) -> boolean

;; determines whether p holds for at least one item

;; on alox

;; (ormap p (list x-1 ... x-n)) =

;; (or (p x-1) (or ... (p x-n)))

(define (ormap p alox) (cond [(empty? alox) false] [else (or (p (first alox))

(ormap p (rest alox)))]))

Page 100: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

102

Lambda the Ultimate…

• According to our recipe, we could abstract, if only and, or were function values rather than special forms …(define (ormap p alox) (cond [(empty? alox) false] [else (or (p (first alox))

(ormap p (rest alox)))]))

(define (andmap p alox) (cond [(empty? alox) true] [else (and (p (first alox))

(andmap p (rest alox)))]))

… the case of andmap and ormap

Page 101: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

103

Lambda the Ultimate…

… the case of andmap and ormap

• In fact, we have already an abstract function that we could reuse for defining andmap and ormap …I.e., we do not need andormap

(define (andormap bool-op init p alox) (cond [(empty? alox) init] [else (bool-op (p (first alox))

(andormap bool-op init p (rest alox)))]))

(define (andmap3 p l) (andormap my-and true p l))

(define (ormap3 p l) (andormap my-or false p l))

Page 102: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

104

Lambda the Ultimate…

(define (prod alon) (fold 1 * alon))(define (sum alon) (fold 0 + alon))

(define (andmap4 p alox) (fold true my-and (map p alox)))

(define (ormap4 p alox) (fold false my-or (map p alox)))

;; fold : Y (X Y -> Y) (listof X) -> Y (define (fold init combine-op lst) (cond [(empty? lst) init] [else (combine-op (first lst) (fold init combine-op (rest lst)))]))

Page 103: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

105

Outline

• Similarities in definitions• Functions are values• Designing abstractions from examples• Designing abstractions with functions as

values• Defining functions on the fly• “pipes-and-filters” organization of

computations

Page 104: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

106

[Reminder: The role of abstraction]

• Programmers can reduce the maintenance cost by organizing programs correctly

• First principle: match the structure of the function structure to the structure of its input data. – This rule makes it easy to modify and extend functions

when the set of possible input data changes• Second principle: introduce proper

abstractions– Every abstracted function creates a single point of

control for at least two different functions, often for several more

Now that we have higher-order functions, it is time to reconsider the first principle more carefully …

Page 105: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

107

Process structure follows data …

Summing the Squares of odd nodes in a tree(define (sum-of-odd-squares tree) (cond [(empty? tree) 0]

[(not (cons? tree)) (if (odd? tree) (square tree) 0)]

[else (+ (sum-of-odd-squares (first tree)) (sum-of-odd-squares (rest tree)))]))

Page 106: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

108

Process structure follows data …

(define (even-fibs n) (local ((define (process k) (cond [(> k n) empty] [else (local ((define f (fib k))) (cond [(even? f) (cons f (process (+ k 1)))] [else (process (+ k 1))]))]))) (process 0)))

Collecting the even fibonacci numbers of 0..n 0 if n = 0

Fib(n) = 1 if n = 1Fib(n-1) + Fib(n-2) otherwise

Page 107: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

109

Process structure follows data …

(define (sum-of-odd-squares tree) (cond [(empty? tree) 0] [(not (cons? tree)) (if (odd? tree) (square tree) 0)] [else (+ (sum-of-odd-squares (first tree)) (sum-of-odd-squares (rest tree))

) ] ) )

(define (even-fibs n) (local ((define (process k) (cond [(> k n) empty] [else (local ((define f (fib k))) (cond [(even? f) (cons f (process (+ k 1)))] [else (process (+ k 1))]))]))) (process 0)))

Do you recognizeany similarity between the corresponding processes ?

Page 108: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

110

Focusing on the process structure…

A more abstract description of the computations reveals…– sum-of-odd-squares:

• enumerates the leaves of a tree• filters them to select the odd ones• processes each of the selected ones by squaring • accumulates the results using +, starting with 0

– even-fibs:• enumerates the integers from 0 to n• process each selected integer by computing its Fibonacci

number• filters them to select the even ones• accumulates the results using cons, starting with the empty

list

Page 109: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

111

Focusing on the process structure…

Enumerate:leaves

filter:odd?

Enumerate:numbers

filter:even?

map:fib

map:square

accumulate:+, 0

accumulate:cons, empty

sum-of-odd-squares

list-of-even-fibs

Page 110: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

112

Process structure follows data …• Organizing the process by following the data

structure fails to exhibit the sub-process-flow structure … • There are no distinct parts that correspond to the

elements of the process-flow description; they are mingled together enumeration

accumulationprocessing

filtering

(define (sum-of-odd-squares tree) (cond [(empty? tree) 0]

[(not (cons? tree)) (if (odd? tree) (square tree) 0) ] [else (+ (sum-of-odd-squares (first tree)) (sum-of-odd-squares (rest tree))

) ] ))

Page 111: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

113

Decomposing in stages

• A process can often be decomposed into a sequence of stages, e.g.,– compiling– finding words that are common to two text files

• Common types of stages– Enumerate, filter, transduce, accumulate

1) Make list of all leaves [enumerate]

2) Extract the odd leaves from list [filter]3) Make list of the squares

[transduce]4) Sum up the squares

[accumulate]

Page 112: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

114

a general-purpose filter

(define (filter test alox) (cond [(empty? alox) empty] [(test (first alox)) (cons (first alox) (filter test (rest alox)))] [else (filter test (rest alox))]))

(filter even? (list 4 5 7 2 6 9 10 1)) ->(4 2 6 10)

Abstracting over the test by passing the predicate as a parameter

a template for filtering processes

Page 113: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

115

;; fold : Y (X Y -> Y) (listof X) -> Y (define (fold combine-op init-val alox) (cond [(empty? alox) init-val] [else (combine-op (first alox) (fold combine-op init-val (rest alox)))]))

A general-purpose accumulator

a template for accumulation processes

• Abstracting over the accumulation operator (combine-op), the initial value (init) and over what we accumulate (alox)

• All we know is that we accumulate by applying the operator over the values of a list

Page 114: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

116

Enumerators depend on data structure

• The way we walk through a data structure cannot be independent of the structure of data …

• Enumerating integer intervals…

(enumerate-interval 3 10) --> (3 4 5 6 7 8 9 10)

(define (enumerate-interval lo hi) (cond [(> lo hi) empty] [else (cons lo

(enumerate-interval (+ lo 1) hi))]))

Page 115: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

117

Enumerators depend on data structure

(define (enumerate-tree tree) (cond [(empty? tree) empty] [(not (cons? tree)) (list tree)] [else (append (enumerate-tree (first tree)) (enumerate-tree (rest tree)))]) )

if x --> ((1 3) 2 (5 (4 6))), then(enumerate-tree x) --> (1 3 2 5 4 6)

• Enumerating leaves…

Page 116: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

118

sum-of-odd-squares as stages

[transduce]

[accumulate]

[filter]

enumerationaccumulation

processingfiltering

(sum-of-odd-squares ‘((1 3) 2 (5 (4 6))) )

35

(define (sum-of-odd-squares tree) (fold + 0 (map square (filter odd? (enumerate-tree tree)

) )

))

Page 117: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

119

Other processes based on stages

(define (even-fibs n) (fold cons empty (filter even? (map fib (enumerate-interval 0 n)))))

(define (highest-programmer-salary records) (fold max 0 (map salary (filter programmer? records)) ))

Page 118: Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 5: Abstracting

Dr. G. RößlingProf. Dr. M. Mühlhäuser

RBG / Telekooperation©

Introduction to Computer Science I: T5

120

Advantages of decomposing into stages• Support modular program designs

– Designs are constructed by combining relatively independent pieces (components)

• Most of the stages can be written as general purpose procedures that can serve as templates for a variety of processes

– Easier to write and understand

• Can encourage modular design by – providing a library of standard components– A conventional interface for flexibly connecting the

components

Modular construction is a powerful strategy for controlling complexity in

engineering design

Can you think of disadvantages?