6 Designing with Abstractions
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
Introduction
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)))
Recall the abstraction design recipes.
Creation |
| Use |
|
|
|
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.