On this page:
PARTNER CHANGE
Finite Increasing Arithmetic Sequences
List Abstractions
Signature Detective
8.10

5 Designing With Lists and Abstractions

home work!

Purpose: This lab focuses on designing programs on lists, as well as creating abstractions.

Textbook references:

Part II: Arbitrarily Large Data

Chapter 14: Similarities Everywhere

Chapter 15: Designing Abstractions

PARTNER CHANGE

Goals: Your new lab partner today will be your HW partner for the next several weeks.

Exercise 1 Sit next to your new partner.

Exercise 2 Exchange contact information with your partner: telephone number, latest-greatest social network scheme, whatever-app.

Exercise 3 Agree to a first meeting time and meeting place. At that meeting, agree to next meeting time and meeting place.

Finite Increasing Arithmetic Sequences

Goals: Use abstraction in an unfamiliar context.

Starter Code: Below is the data definition of a finite increasing arithmetic sequence.

(define Positive (signature (predicate (lambda (x) (and (number? x) (positive? x))))))
(define-struct fias [min max step])
(define AFIAS (signature [FiasOf Number Number Positive]))
; where (make-fias min max step) represents all numbers
; of the form min + (k * step), where k is a natural number,
; such that min + (k * step) < max.
 
(define FIAS-EMPTY (make-fias 1 1 0.25)) ; empty sequence, as min >= max
(define FIAS-1 (make-fias 0 1 0.25)) ; sequence with the elements (0, .25, .5, .75)
 
; fias-temp : AFIAS -> ?
(define (fias-temp fias)
  (... (fias-min fias) ... (fias-max fias) ... (fias-step fias) ...))

Sample Problem Design a function that determines if AFIAS is empty.

(: empty-fias? (AFIAS -> Boolean))
; determines if the given FIAS is empty
(define (empty-fias? fias)
  (>= (fias-min fias) (fias-max fias)))

Sample Problem Design the function next-sequence, which takes AFIAS and returns a new AFIAS with the same max and step. The new min will be the original AFIAS’s min with its step added to it.

(: next-sequence (AFIAS -> AFIAS))
; returns a new FIAS where the min is the original FIAS's min plus its step
(define (next-sequence fias)
  (make-fias
    (+ (fias-min fias) (fias-step fias))
    (fias-max fias)
    (fias-step fias)))

Sample Problem You may have noticed that next-sequence returns the rest of a AFIAS, empty-fias? determines if it is empty, and that if it is non-empty fias-min is the first element in it. Redefine your fias-temp to take advantage of this recursive structure.

; fias-temp : AFIAS -> ?
(define (fias-temp fias)
  (cond
    [(empty-fias? fias) ...]
    [else (... (fias-min fias) ... (fias-temp (next-sequence fias)) ...)]))

Exercise 4 Design a function which sums the elements of a AFIAS.

Exercise 5 Design a function which multiplies the elements of a AFIAS. Hint: the product of an empty sequence is 1.

Exercise 6 Design a function which lists the elements of AFIAS.

Exercise 7 All of these functions look marvelously similar; abstract them, redefine them with your new function, and ensure all of your tests still pass.

Exercise 8 Design a function which determines if any element of AFIAS is a perfect square. Hint: integer?.

Exercise 9 Design a function which determines if any element of AFIAS is even.

Exercise 10 Both of these functions look marvelously similar; abstract them, redefine them with your new function, and ensure all of your tests still pass.

List Abstractions

Goals: Practice understanding how to use the foldr list abstraction.
(require 2htdp/image)
 
(define Image (signature (predicate image?)))
(define Color (signature (predicate image-color?)))
 
(define Mode (signature (predicate mode?)))
; is one of
; - "solid"
; - "outline"
 
(define-struct circl [radius mode c])
(define-struct squar [side-length mode c])
(define-struct rectangl [width height mode c])
(define Shape (signature (mixed [CirclOf Number Mode Color]
                                [SquarOf Number Mode Color]
                                [RectanglOf Number Number Mode Color])))
; and represents either:
; - the radius in pixels, mode, and color of a circle
; - the side length in pixels, mode, and color of a square
; - the width and height in pixels, the mode, and color of a rectangle

Exercise 11 Provide examples and templates for the above data definitions.

Exercise 12 Use filter to design a function that takes a [ListOf Shape] and keeps only circles.

Exercise 13 Use foldr to design a function that takes a [ListOf Shape] and stacks all of its images vertically (hint: you may need to use map as well).

Signature Detective

Goals: Practice understanding what the data type restrictions on code must be when reading it.

Exercise 14 Determine the signature for the following functions.

(define (a supercut of us)
  (+ of
     (if (empty? supercut)
         (us #f)
         (us (first supercut)))))
 
(define (the moments i play in the dark)
  (play (in (play the dark)) (play moments i)))
 
(define (come home to my heart)
  (cond [(home my) (to heart)]
        [(my heart) " "]
        [else ""]))