09-funcabst-post3up

09-funcabst-post3up - Functional abstraction Readings:...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Functional abstraction Readings: HtDP, sections 19-24. Language level: Intermediate Student With Lambda different order used in lecture section 24 material introduced much earlier sections 22, 23 not covered in lecture CS 135 Fall 2009 09: Functional abstraction 1 What is abstraction? Abstraction is the process of finding similarities or common aspects, and forgetting unimportant differences. Example: writing a function. The differences in parameter values are forgotten, and the similarity is captured in the function body. We have seen many similarities between functions, and captured them in design recipes. But some similarities still elude us. CS 135 Fall 2009 09: Functional abstraction 2 Eating apples ( define ( eat-apples lst ) ( cond [( empty? lst ) empty ] [( cons? lst ) ( cond [( not ( symbol = ? ( first lst ) apple )) ( cons ( first lst ) ( eat-apples ( rest lst )))] [ else ( eat-apples ( rest lst ))])])) CS 135 Fall 2009 09: Functional abstraction 3 Keeping odd numbers ( define ( keep-odds lst ) ( cond [( empty? lst ) empty ] [( cons? lst ) ( cond [( odd? ( first lst )) ( cons ( first lst ) ( keep-odds ( rest lst )))] [ else ( keep-odds ( rest lst ))])])) CS 135 Fall 2009 09: Functional abstraction 4 Abstracting from these examples What these two functions have in common is their general structure. Where they differ is in the specific predicate used to decide whether an item is removed from the answer or not. We could write one function to do both these tasks if we could supply, as an argument to that function, the predicate to be used. The Intermediate language permits this. CS 135 Fall 2009 09: Functional abstraction 5 In the Intermediate language, functions are values. In fact, they are first-class values. They have the same status as the other values weve seen. They can be bound to variables, put in lists and structures, consumed as arguments, and produced as results. This is a feature that many mainstream programming languages (e.g. Java, C, C++) lack. This feature is central to functional programming languages. CS 135 Fall 2009 09: Functional abstraction 6 More rapidly-evolving or recently-defined languages that are not primarily functional (e.g. Python, Ruby, Perl 6, C#) do implement this feature to some extent. Java and C++ provide other abstraction mechanisms that provide some of the benefits. Functions-as-values provides a clean way to think about the concepts and issues involved in abstraction. You can then worry about how to implement a high-level design in a given programming language. CS 135 Fall 2009 09: Functional abstraction 7 Consuming functions ( define ( my-filter pred lst ) ( cond [( empty? lst ) empty ] [( cons? lst ) ( cond [( pred ( first lst )) ( cons ( first lst ) ( my-filter pred ( rest lst )))] [ else ( my-filter pred ( rest lst ))])])) CS 135 Fall 2009 09: Functional abstraction 8 We can simplify the code somewhat....
View Full Document

Page1 / 29

09-funcabst-post3up - Functional abstraction Readings:...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online