On this page:
Finite Increasing Arithmetic Sequence
Before you go...
Signature Detective
Clicking:   Extreme Edition
6.6

Lab 6 Creating Abstractions

lab!

Purpose: This lab focuses on the creation of abstractions.

Textbook References: Chapter 14: Similarities Everywhere, Chapter 15: Designing Abstractions

Finite Increasing Arithmetic Sequence

Goals: Use abstraction in an unfamiliar context.

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

; A FIAS (Finite Increasing Arithmetic Sequence) is a:
; (make-fias Number Number Positive)
(define-struct fias [min max step])
; 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 : FIAS -> ?
(define (fias-temp fias)
  (... (fias-min fias) (fias-max fias) (fias-step fias)))

Exercise (Reviewed) 1 * Design a function that determines if a FIAS is empty.

Exercise (Reviewed) 2 * Design the function next-sequence, which takes a FIAS and returns a new FIAS with the same max and step. The new min will be the original FIAS’s min with its step added to it.

Exercise (Reviewed) 3 ** You may have noticed that next-sequence returns the rest of a FIAS, 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.

Exercise 4 * Design a function which sums the elements of a FIAS.

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

Exercise 6 * Design a function which lists the elements of a FIAS.

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.

Note: Do not complete the following functions with the above abstraction. The above abstraction does not short-circuit, whereas the following functions should.

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

Exercise 9 * Design a function which determines if any element of a FIAS 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.

Before you go...

If you had trouble finishing any of the exercises in the lab or homework, or just feel like you’re struggling with any of the class material, please feel free to come to office hours and talk to a TA or tutor for additional assistance.

Signature Detective

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

Exercise 11 *** 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 ""]))

Clicking: Extreme Edition

Goals: Using newfound programming abilities to make more interesting programs.

Exercise 12 *** Re-visit your click-the-midpoint game from Lab 3 Structured Data. Redesign it so the player is tasked with clicking the midpoint of a list of points instead of just two. Drawing lines between the list of points is not recommended, as they won’t cross the midpoint (but a line from the midpoint to where the player clicked should still be there).