Module 8

Module 8 - Functional abstraction Readings: HtDP, sections...

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. We will cover this material in a somewhat different order than the text; in particular, some of the material in section 24 is introduced much earlier in lecture. Section 22 contains applications to graphical user interfaces, and section 23 contains mathematical examples that will be familiar to you. We will not be covering this material in lecture, but it is a good source of additional examples and exercises. CS 135 Fall 2008 08: 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 2008 08: Functional abstraction 2 Eating apples ( define ( eat-apples lst ) ( cond [( empty? lst ) empty ] [( cons? lst ) ( cond [( symbol = ? ( first lst ) apple ) ( eat-apples ( rest lst ))] [ else ( cons ( first lst ) ( eat-apples ( rest lst )))])])) CS 135 Fall 2008 08: Functional abstraction 3 Removing even numbers ( define ( remove-even lst ) ( cond [( empty? lst ) empty ] [( cons? lst ) ( cond [( even? ( first lst )) ( remove-even ( rest lst ))] [ else ( cons ( first lst ) ( remove-even ( rest lst )))])])) CS 135 Fall 2008 08: 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 2008 08: 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. CS 135 Fall 2008 08: Functional abstraction 6 More rapidly-evolving or recently-defined languages (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....
View Full Document

Page1 / 24

Module 8 - Functional abstraction Readings: HtDP, sections...

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