Introduction to Computer Science I Topic 5: Abstracting Design
description
Transcript of Introduction to Computer Science I Topic 5: Abstracting Design
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
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
2
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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 repetitions – The role of higher-order procedures in the abstraction
process3
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
4
Get your data structures correct first, and the rest of the program will write itself.
David Jones
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Two similar functions
5
;; 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 ))])]))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Two similar functions
6
;; 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))])]))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
A more abstract function• The more abstract function below requires an
additional parameter: the symbol that we are looking for– It is otherwise like the two original functions
7
;; 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))])]))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
8
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Two other similar functions
9
;; 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
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Two other similar functions
10
greater-than
;; greater-than: lon number -> lon;; to construct a list of those numbers ;; in alon that are greater than threshold 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)])]))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Abstracting over the two functions
11
• The additional parameter, rel-op, of filter1 stands for both concrete relational operators in less-than and greater-than:
(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)])]))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Original functions from abstraction
12
• 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;; 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))
;; less-or-equal: lon number -> lon (define (less-or-equal alon t) (filter1 <= alon t))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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.
13
;; squared>? : number number -> boolean (define (squared>? x c) (> (* x x) c))
(filter1 squared>? (list 1 2 3 4 5) 10)
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Similarities in Data Definitions• Given the similarity between the data
definitions, functions that use elements of these classes of data are similar, too.
14
A list-of-numbers is • either empty • or (cons n l)
• n is a number• l is a list-of-numbers
A list-of-IRs is• either empty • or (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
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Similarities in data definitions
15
;; 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)])]))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Similarities in data definitions
16
;; <ir : IR number -> boolean
(define (<ir ir p) (< (ir-price ir) p))
;; 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)])]))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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:
17
• 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– More abstract: … as long as we can define a function
that compares list of items with items passed to filter1 as the second argument
– Have a look at the following function…
(define (less-than-ir1 aloir t)(filter1 <ir aloir t))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Polymorphic functions
18
;; 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
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Parametric data definitions• Question: How do we write precise contracts for
polymorphic functions such as filter1?• Answer: Use data definitiions with parameters, so-called
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
19
• An arbitrary collection of Scheme data: symbol, IRs, etc.
• Replace ITEM with one of the following names 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 • or (cons s l)
• s is an ITEM • l is a list of ITEM.
TYPE VARIABLE
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Contracts for polymorphic functions• In defining contracts, the abbreviation (listof X) states that a function works on all lists:
20
;; 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
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
21
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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– 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 lecture, 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
22
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Procedures as general methods• First-order procedures: make procedures
independent of particular data involved (numbers, symbols etc.)
• 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 23
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Two violations of the basic language definition
24
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 ...)
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Two violations of the basic language definition
Violation 2:
25
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)])]))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Adjusting basic Scheme1. 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
26
<exp> = <var>| <con> | <prm> | <fct>
| (<exp> <exp> ...<exp>)
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Adjusting basic Scheme• The accommodation of higher-order functions
does not lengthen the grammar; it rather makes it simpler!
2727
<exp> = <var> | <con>| <prm> | <fct> | (<exp> <exp> ...<exp>)
<exp> = <var> | <con> | (<prm> <exp> ...<exp>) | (<fct> <exp> ...<exp>)
Caution: still simplified; many substitutions are invalid!
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
28
<val> = <boo> | <sym> | <num> | empty | <lst> | <var> (names of defined functions)| <prm> | <fct>
<lst> = empty | (cons <val> <lst>)
<val> = <con>
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
29
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Contracts for abstractand polymorphic functions
• We do have contracts for first-order functions, e.g.,
30
;; rel-op : number number -> boolean
• We say that “” describes a class of values: the class of 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 – They are functions “from A and B to C”
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Example contract definitions
31
;; 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.
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Applying functions of functions Does this
application make sense?
32
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.
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
A new use for filter1• Using filter1 to extract all toys with the same name from
a list of inventory records:
33
;; 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 class of data: filter1 : (X TH -> boolean) (listof X) TH -> (listof X)
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
34
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
35
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
36
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Comparing examples
37
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 …
;; convertCF : lon -> lon (define (convertCF alon) (cond [(empty? alon) empty] [else (cons (C->F (first alon)) (convertCF (rest alon))]))
;; names : loIR -> los (define (names aloIR) (cond [(empty? aloIR) empty] [else (cons (IR-name (first aloIR)) (names (rest aloIR)))]))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
38
(define (convertCF f alon) (cond [(empty? alon) empty] [else (cons (f (first alon)) (convertCF (rest alon)))]))
(define (names f aloIR) (cond [(empty? aloIR) empty] [else (cons (f (first aloIR)) (names (rest aloIR)))]))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
39
map captures a common pattern for operations over lists it abstracts over the operation to be performed on the elements of the list
(define (map f lon) (cond [(empty? lon) empty] [else (cons (f (first lon)) (map f (rest lon)))]))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
40
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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:
41
(define (f-from-abstract x) (f-abstract boxed-value x))
(f-from-abstract V) = (f-original V)
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
42
;; 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))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
43
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
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Formulating general contracts• By looking at the definition, we can see:
– 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:
44
map : (X -> Y) (listof X) -> (listof Y)
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Contracts and new uses of abstract functions
• An abstract function is generally useful in a much broader context than we first anticipated: – For example, map can be used whenever we need to
produce a new list by processing all items on an existing list
45
;; 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)
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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 have 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
46
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
[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…
48
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Higher-order functions as abstraction barriers
by the example of map
49
(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:1. 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
2. The operation to apply to each element is hard-coded
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Higher-order functions as abstraction barriers
by the example of map
50
(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!
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Higher-order functions as abstraction barriers
by the example of map• An abstraction barrier isolates the implementation of procedures that transform lists from the details of how the elements of the list are extracted and combined
51
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
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
53
(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 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))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Abstracting from templates: fold
54
• fold captures a common pattern for processing lists• applying a binary operation to the first element of the list and
the result of fold to the rest of the list;; 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))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
[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
55
;; 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) ...)
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Deriving sort from fold
56
Can we use fold to sort lists?
Recall insertion sort…
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
[Recall insert]
57
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 n alon) (cond [(empty? alon) (cons n empty)] [(<= n (first alon)) (cons n alon)] [else (cons (first alon) (insert n (rest alon)))]))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
[Recall: insertion sort]
58
;; 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
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Deriving sort from fold
59
;; 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
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
sort @ work
60
(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
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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 61
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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 (e.g., calendar dates)
• There are estimations that maintenance makes up most of the lifetime of successful programs (60-80%)
62
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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 – The second modification uses a local-expression to
make the nested cond-expression more readable
63
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Why “single point of control”?
64
(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)]))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Why “single point of control”?
65
(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)])]))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
66
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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.
67
Guideline on Creating Abstractions:Form an abstraction instead of copying and modifying a piece of a program.
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
68
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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 69
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
70
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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 71
(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
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Creating functions with “memory”
• The most interesting property of add:– Its result “remembers” the value of the argument x
at application time – Each time we use the result of applying add, it uses
the value of x at application time of add
72
;; 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
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Creating functions with “memory”
73
x5
4 95 2 7
8 82 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)
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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 variables74
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Abstracting with functions with memory
75
;; 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)])]))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Abstracting with functions with memory
76
;; 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)])]))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Abstracting with functions with memory
77
(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.
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Alternative abstraction recipeThe 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 78
(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))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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:
79
(define less-than2 (filter2 <)) (define greater-than2 (filter2 >))
;; filter2 : (X Y -> boolean) -> ((listof X) Y -> (listof X))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
80
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Motivating on the fly function definition
• Many uses of abstract functions often require the definition of auxiliary functions – filter1 can be used with <ir, eq-ir, … <your function>
81
;; 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:
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Motivating on the fly function definition
If auxiliary functions are only used as arguments to some abstract function f, we use a local-expression
82
;; 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))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
83
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
84
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)))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
85
(lambda (x-1 ... x-n) exp)
(local ((define (a-new-name x-1 ... x-n) exp)) a-new-name)
a-new-name must not occur in exp
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Using lambda instead of local
86
;; 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))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
lambda instead of local
87
;; 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
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Using lambda instead of local
88
Quiz: Can you replace local with lambda here?
(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))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
89
(lambda (x-1 ... x-n) exp)
(local ((define (a-new-name x-1 ... x-n) exp)) a-new-name)
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Semantics of lambdaTo illustrate, let us evaluate the following
application:
90
(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)
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Scope and Semantics of lambda
91
(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
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Semantics of lambdaWe can also explain the semantics of lambda
directly:
92
= exp mit x-1 ... x-n ersetzt durch val-1 ... val-n
( (lambda (x-1 ... x-n) exp) val-1 ... val-n )
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Semantics of lambdaTo illustrate, let’s evaluate the application again
93
(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)
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Scope and Semantics of lambda
94
(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)]) = ...
1. El. der Liste
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Pragmatics of lambda
95
Guideline on Lambda Expressions Use lambda-expressions when a function is not recursive and is only needed once as an argument
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
96
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
97
(define my-or (lambda (op1 op2) (cond [op1 true] [else (cond [op2 true] [else false])])))
(define my-and (lambda (op1 op2) (cond [op1 (cond [op2 true] [else false])] [else false])))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Lambda the Ultimate…
• Special forms cannot be passed as parameters …
• For illustration, consider Scheme functions andmap and ormap
98
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!
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Lambda the Ultimate…
99
… the case of andmap and ormap
(andmap even? `(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)))] ))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Lambda the Ultimate…
100
… 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)))]))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Lambda the Ultimate…
According to our recipe, we could abstract, if only and, or were function values rather than special forms …
101
(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
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Lambda the Ultimate…
102
… 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))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Lambda the Ultimate…
103
(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)))]))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
104
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
[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
105
Now that we have higher-order functions, it is time to reconsider the first principle more carefully …
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Process structure follows data …
106
(define (sum-of-odd-squares alon) (cond [(empty? alon) 0]
[(not (cons? alon)) (if (odd? alon) (square alon) 0)]
[else (+ (sum-of-odd-squares (first alon)) (sum-of-odd-squares (rest alon)))]))
Process structure follows data
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Collecting the even fibonacci numbers of 0..n
107
0 für n = 0fib(n) = 1 für n = 1
fib(n-1) + fib(n-2) sonst
Process structure follows data;; sammelt die geraden Fib(k) für k = [0..n] (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)))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Process structure follows data …
108
(define (sum-of-odd-squares tree) (cond [(empty? tree) 0] [(not (cons? tree)) (if (odd? tree) (sqr tree) 0)] [else (+ (sum-of-odd-squares (first tree)) (sum-of-odd-squares (rest tree))
) ] ))
Do you recognize similarities between the corresponding processes?
(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)))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Focusing on the process phases…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
109
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Focusing on the process phases…
110
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
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Process structure follows data …
111
• 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
enumerationaccumulation
processingfiltering
(define (sum-of-odd-squares tree) (cond [(empty? tree) 0] [(not (cons? tree)) (if (odd? tree) (sqr tree) 0)] [else (+ (sum-of-odd-squares (first tree)) (sum-of-odd-squares (rest tree))
) ] ))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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
112
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]
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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))]))
113
(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
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
;; 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
114
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
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Enumerators depend on data structure
• The way we walk through a data structure cannot be independent of the structure of data …
• Enumerating integer intervals…
115
(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))]))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
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)))]) )
116
if x --> ((1 3) 2 (5 (4 6))), then(enumerate-tree x) --> (1 3 2 5 4 6)
• Enumerating leaves…
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
sum-of-odd-squares as stages
117
[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 sqr (filter odd? (enumerate-tree tree)
) )
))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Other processes based on stages
(define (even-fibs n) (fold cons empty (filter even? (map fib (enumerate-interval 0 n)))))
118
(define (highest-programmer-salary records) (fold max 0 (map salary (filter programmer? records)) ))
Dr. G. RößlingProf. Dr. M. Mühlhäuser
RBG / Telekooperation©
Introduction to Computer Science I: T5
Advantages of decomposing into stages• Supports 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
119
Modular construction is a powerful strategy for controlling complexity in
engineering design
Can you think of disadvantages?