On this page:
Creating Abstractions
Code Review
Existing Abstractions
Warm Up
Code Review

6 Designing with Abstractions

home work!

Purpose The purpose of this lab is to recognize opportunities for the use of existing abstractions in existing code and to simplify the code accordingly.

Textbook references Part III: Abstraction


15 minutes

(define (new-function k l ...)
  (local (; X = ..., Y = ...,
          ; derive signature for helper function
          ; ... -> ...
          ; purpose statement for helper
          (define (helper ...) default-value))
   (existing-abstraction k l ... helper)))

Figure 1: The template for abstraction re-use.

Recall the abstraction design recipes.




  • Find the corresponding differences. Circle them and connect them with lines.

  • Replace the differences with names and uses these names as parameters to the functions.

    Don’t forget to pass along the new parameters in the recursive calls.

    Now you should have two functions that are the same except for their names.

  • Formulate the original functions in terms of the new one.

    Add a suffix to the name of the originally defined functions (say -v0).

    This reuses the tests from the original function to ensure the workings of the abstraction.


  • Follow the normal 6-step design process.

  • If you recognize that the function can be designed with an existing abstraction,

    • Match the signatures and purpose statements.

    • Create a local template. See figure 1.

    • Design the rest of the helper function as before. Derive the examples for helper from the examples for new-function. skip the testing.

  • Don’t forget to test (as before).

If you really want to make sure that you abstracted properly, you come up with a completely different function that can be implemented with this new abstraction.

Creating Abstractions

15 minutes

Exercise 1 Analyze the similarities and differences between the following two functions. Create an abstracted function and re-write them. This will give you your own homemade foldr.

(require 2htdp/image)
; [List-of String] -> String
; concatenates all the strings in a given list into one string
(check-expect (collapse (list "hello" " " "claudia"))
              "hello claudia")
(define (collapse ls)
  (cond [(empty? ls) ""]
        [else (string-append (first ls) (collapse (rest ls)))]))
; [List-of Image] -> Image
; draws all images in list placed horizontally next to each other
(define c (circle 3 'solid 'red))
(define s (square 3 'solid 'blue))
(check-expect (h-cat (list c s)) (beside/align "bottom" c s))
(define (h-cat li)
  (cond [(empty? li) empty-image]
        [else (beside/align "bottom" (first li) (h-cat (rest li)))]))

Code Review

15 minutes

The Teaching Assistants will pick a pair who will explain a solution to their peers following the design recipe steps.

Existing Abstractions

30 minutes

Exercise 2 Download the code for a bubble simulator, which we created to aid with your relaxation.

The code will be made available separately.

The simulator’s main function starts with a given initial number of bubbles and the user can add extra bubbles by clicking the mouse. New bubbles will grow in size for the first few clock of their lifetime and old bubbles will pop and disappear. The bubbles move randomly across the screen and come in two colors (but you can increase the number of colors).

Review the code. Use existing ISL abstractions to refine the code.

Competition For decades, companies measured the productivity of developers by the number of lines of code they produced. A moment’s thought should tell you why this measure is blatantly absurd.

Nowadays companies are measuring the number of lines removed without reducing or violating the functionality of the software.

In this spirit, we will reward the pair of "A I developers" who find the most uses of existing abstractions to simplify the given code. If you wish to participate in this optional competition follow these guidelines:
  • Document each use of an abstraction in a separate section between the purpose statement of the entire program and the CONSTANTS section.

  • Each separate entry of this change log must start with the name of the function that you modified.

  • Separate the entries by a blank line.

  • Upload your solution before the start of the code walk to the place marked "lab 6".

Your entry will be eliminated without further checking if you violate these guidelines.

Warm Up

If you find the above exercise too much to get started, here’s a starter exercise.

Exercise 3 Design the function format-list using existing abstractions. It takes in a list of lists and formats it so that all empty lists are removed and the lists are ordered from longest to shortest.

Code Review

15 minutes

The Teaching Assistants will pick two pairs who will explain a solution to their lab mates following the design recipe steps.