Download presentation

Presentation is loading. Please wait.

Published byParker Mackley Modified over 2 years ago

1
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

2
Dr. G. Rößling Prof. 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

3
Dr. G. Rößling Prof. 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 them –The role of higher-order procedures in the abstraction process 3

4
Dr. G. Rößling Prof. 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

5
Dr. G. Rößling Prof. 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 ))])]))

6
Dr. G. Rößling Prof. 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))])]))

7
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 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 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))])]))

8
Dr. G. Rößling Prof. 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

9
Dr. G. Rößling Prof. 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

10
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Two other similar functions 10 ;; 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

11
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Abstracting over the two functions 11 (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 : update: rel-op -> rel-op

12
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Original functions from abstraction 12 ;; 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

13
Dr. G. Rößling Prof. 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)

14
Dr. G. Rößling Prof. 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 are similar, too. 14 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

15
Dr. G. Rößling Prof. 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)])]))

16
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Similarities in data definitions 16 ;; 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)])])) ;; boolean (define (< ir ir p) (< (ir-price ir) p))

17
Dr. G. Rößling Prof. 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 (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 …

18
Dr. G. Rößling Prof. 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

19
Dr. G. Rößling Prof. 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: 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 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

20
Dr. G. Rößling Prof. 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

21
Dr. G. Rößling Prof. 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

22
Dr. G. Rößling Prof. 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: 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 22

23
Dr. G. Rößling Prof. 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 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 Schemes 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

24
Dr. G. Rößling Prof. 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))) ==== x | alon |... area-of-disk | perimeter |... Function names as arguments in applications imporant update:grammar had issues, i.e. Reason: An argument is an expression and the class of expressions does not include function names = | | (... ) | (cond ( )...( )) | (cond ( )...(else )) | (local...)

25
Dr. G. Rößling Prof. 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 ( ) and primitive operations ( ) 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)])]))

26
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Adjusting basic Scheme 1.Include the names of functions and primitive operations in the definition of 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 = | | | | (... ) update: added in Grammar

27
Dr. G. Rößling Prof. 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! 27 = | | | | (... ) = | | (... )

28
Dr. G. Rößling Prof. 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 = | | | empty | | (names of defined functions) | | =empty | (cons ) =

29
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Intermediate Student Scheme Grammar 29 =(define (... ) ) | (define ) | (define-struct (... )) = | | | | | | | | (... ) | (cond ( )...( )) | (cond ( )...(else )) | (local (... ) ) ==== x | alon |... area-of-disk | circumference |... = true | false = 'a | 'doll | 'sum |... = 1 | -1 | 3/5 | 1.22 |... = + | - | cons | first | rest |...

30
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Contracts for abstract and 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 30

31
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Contracts for abstract and polymorphic functions But, we do have contracts for first-order functions, e.g., 31 ;; 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

32
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Example contract definitions First version of filter1 32 ;; 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 filter1 filter1 : (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.

33
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Applying functions of functions Does this application make sense? 33 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.

34
Dr. G. Rößling Prof. 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: 34 ;; 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)

35
Dr. G. Rößling Prof. 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 35

36
Dr. G. Rößling Prof. 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 36

37
Dr. G. Rößling Prof. 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 boxes 2.Abstract if boxes contain values 3.Test the validity of the abstraction 4.Formulate the contract of the abstraction 37

38
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Comparing examples 38 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)))]))

39
Dr. G. Rößling Prof. 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 39 (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)))]))

40
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Two steps of the abstraction 2.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 40 (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

41
Dr. G. Rößling Prof. 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 41

42
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Defining the original functions in 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: 42 (define (f-from-abstract x) (f-abstract boxed-value x)) (f-from-abstract V) = (f-original V)

43
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Defining the original functions in 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 43 ;; 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))

44
Dr. G. Rößling Prof. 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 44 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

45
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 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 Y s, map produces a list of Y s: 45 map : (X -> Y) (listof X) -> (listof Y)

46
Dr. G. Rößling Prof. 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 anticipate: –For example, map can be used whenever we need to produce a new list by processing all items on an existing list 46 ;; 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)

47
Dr. G. Rößling Prof. 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 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 47

48
Dr. G. Rößling Prof. 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 48 (number -> number) (listof number) -> (listof number) (IR -> symbol) (listof IR) -> (listof symbol) (X -> Y) (listof X) -> (listof Y) XY

49
Dr. G. Rößling Prof. 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 … 49

50
Dr. G. Rößling Prof. 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 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

51
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Higher-order functions as abstraction barriers by the example of map 51 (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!

52
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 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 52 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

53
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Using map for trees ;; scale-tree : (treeof numbers) number -> (treeof numbers) ;; constructs a new tree by multiplying ;; each element of tree by num (define (scale-tree tree num) (map (local ((define (scale-st sub-tree) (cond [(cons? sub-tree) (scale-tree sub-tree num)] [else (* sub-tree num)]))) scale-st) tree) ) 53 map together with recursion is a powerful abstraction for processing tree data structures

54
Dr. G. Rößling Prof. 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 54 (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))

55
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Abstracting from templates: fold 55 ;; 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

56
Dr. G. Rößling Prof. 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 56 ;; 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)...)

57
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Deriving sort from fold 57 Can we use fold to sort lists? Recall insertion sort…

58
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 [Recall insert ] 58 el i, i = [1… j-1], el i <= an el j el j+1... el n alon an Insert for non-empty alon : bypass alon elements until the first element, e j, that is larger than an. Insert an between el j-1 and el j ;; 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)))]))

59
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 [Recall: insertion sort ] 59 ;; 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)))])) an sortedunsorted

60
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Deriving sort from fold 60 ;; 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

61
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 sort @ work 61 (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 328

62
Dr. G. Rößling Prof. 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 62

63
Dr. G. Rößling Prof. 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 (for example, calendar dates) There are estimations that maintenance makes up most of the lifetime of successful programs (60-80%) 63

64
Dr. G. Rößling Prof. 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 Something that an experienced programmer may wish to do –The second modification uses a local -expression to make the nested cond -expression more readable 64

65
Dr. G. Rößling Prof. 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 (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)]))

66
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Why single point of control? 66 (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)])]))

67
Dr. G. Rößling Prof. 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 67

68
Dr. G. Rößling Prof. 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. 68 Guideline on Creating Abstractions: Form an abstraction instead of copying and modifying a piece of a program.

69
Dr. G. Rößling Prof. 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 69

70
Dr. G. Rößling Prof. 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 70

71
Dr. G. Rößling Prof. 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 71

72
Dr. G. Rößling Prof. 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 functions arguments at application time 72 (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

73
Dr. G. Rößling Prof. 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 –Each time we use the result of applying add, it uses the value of x at application time of add 73 ;; 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

74
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Creating functions with memory 74 x 5 4 9 5 2 7 8 8 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)

75
Dr. G. Rößling Prof. 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 variables 75

76
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Abstracting with functions with memory 76 ;; 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)])]))

77
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Abstracting with functions with memory 77 ;; 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)])]))

78
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Abstracting with functions with memory 78 (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.

79
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 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 79 (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))

80
Dr. G. Rößling Prof. 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 : 80 (define less-than2 (filter2 <)) (define greater-than2 (filter2 >)) ;; filter2 : (X Y -> boolean) -> ((listof X) Y -> (listof X))

81
Dr. G. Rößling Prof. 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 81

82
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Motivating on the fly function definition Many uses of abstract functions require the definition of auxiliary functions –filter1 can be used with 82 ;; 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:

83
Dr. G. Rößling Prof. 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 83 ;; 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 (

84
Dr. G. Rößling Prof. 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 84

85
Dr. G. Rößling Prof. 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 85 1.(lambda (x c) (> (* x x) c)) 2.(lambda (ir p) (< (ir-price ir) p)) 3.(lambda (ir p) (symbol=? (ir-name ir) p)) =(lambda (... ) ) (define (plus4 x) (+ x 4)) is equivalent to (define plus4 (lambda (x) (+ x 4)))

86
Dr. G. Rößling Prof. 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 86 (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

87
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Using lambda instead of local 87 ;; find : list-of-IRs number -> boolean (define (find aloir t) (filter1 (local ((define (

88
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 lambda instead of local 88 ;; 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 returned Why the indirection? Better: directly create and return the procedure value

89
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Using lambda instead of local 89 (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?

90
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 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 90 (lambda (x-1... x-n) exp) (local ((define (a-new-name x-1... x-n) exp)) a-new-name)

91
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Semantics of lambda To illustrate, let us evaluate the following application: 91 (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)

92
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Semantics of lambda 92 (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

93
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Semantics of lambda We can also explain the semantics of lambda directly: 93 ((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

94
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Semantics of lambda To illustrate, lets evaluate the application again 94 (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)

95
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Semantics of lambda 95 (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

96
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Pragmatics of lambda 96 Guideline on Lambda Expressions Use lambda-expressions when a function is not recursive and is only needed once as an argument

97
Dr. G. Rößling Prof. 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 97

98
Dr. G. Rößling Prof. 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 98 (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])])))

99
Dr. G. Rößling Prof. 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 99 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!

100
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Lambda the Ultimate… 100 … 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)))]))

101
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Lambda the Ultimate… 101 … 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)))]))

102
Dr. G. Rößling Prof. 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 … 102 (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

103
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Lambda the Ultimate… 103 … 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))

104
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Lambda the Ultimate… 104 (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)))])) update: contract of fold wasnt correct and l-> lst for (readability)

105
Dr. G. Rößling Prof. 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 105

106
Dr. G. Rößling Prof. 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 106 Now that we have higher-order functions, it is time to reconsider the first principle more carefully …

107
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Process structure follows data … 107 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)))]))

108
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Process structure follows data … 108 (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 0if n = 0 Fib(n) = 1if n = 1 Fib(n-1) + Fib(n-2)otherwise

109
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Process structure follows data … 109 (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 recognize any similarity between the corresponding processes ?

110
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 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 110

111
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Focusing on the process structure… 111 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

112
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 Process structure follows data … 112 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 accumulation processing 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)) ) ] )

113
Dr. G. Rößling Prof. 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 113 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]

114
Dr. G. Rößling Prof. 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))])) 114 (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

115
Dr. G. Rößling Prof. 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 115 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

116
Dr. G. Rößling Prof. 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… 116 (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))]))

117
Dr. G. Rößling Prof. 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)))]) ) 117 if x --> ((1 3) 2 (5 (4 6))), then (enumerate-tree x) --> (1 3 2 5 4 6) Enumerating leaves…

118
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 sum-of-odd-squares as stages 118 [transduce] [accumulate] [filter] enumeration accumulation processing filtering (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) )

119
Dr. G. Rößling Prof. 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))))) 119 (define (highest-programmer-salary records) (fold max 0 (map salary (filter programmer? records)) )

120
Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T5 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 120 Modular construction is a powerful strategy for controlling complexity in engineering design Can you think of disadvantages?

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google