On this page:
Designing Abstractions
Understanding Abstractions
Before You Go...
7.9

Lab 6 Designing and Using Abstractions

lab!

Purpose: This lab will give you practice using existing list abstractions.

Textbook references: Chapter 14: Similarities Everywhere, Chapter 15: Designing Abstractions, Chapter 16: Using Abstractions

Designing Abstractions

Exercise 1 Design the function merge-layers, which, like the command in Photoshop, merges a number of image layers together into one image. Any number of image layers is represented by a list of images. Here is some data to get started:
(define lst-tri (list (triangle 20 "solid" "green")
                      (triangle 35 "solid" "red")
                      (triangle 45 "solid" "orange")
                      (triangle 55 "solid" "blue")))
; Using lst-tri in our merge-layers function should output something like:
image

Exercise 2 Consider the following function definitions:
; matching-x-posn : [List-of Posn] Number Posn -> Posn
; Find the first Posn in the list with the given x-coordinate or return the given Posn
; if no such position can be found
(check-expect (matching-x-posn '() 10 (make-posn 0 0)) (make-posn 0 0))
(check-expect
 (matching-x-posn
  (cons (make-posn 1 2) (cons (make-posn 3 4) '())) 3 (make-posn 5 6))
 (make-posn 3 4))
(define (matching-x-posn lop desired-x default)
  (cond [(empty? lop) default]
        [(cons? lop)
         (if (= (posn-x (first lop)) desired-x)
             (first lop)
             (matching-x-posn (rest lop) desired-x default))]))
 
; string-with-length : [List-of String] Nat -> String
; Returns the first String in the given list with the given length or "no such string" if no
; such string can be found
(check-expect (string-with-length '() 10) "no such string")
(check-expect (string-with-length (cons "hi" (cons "hello" (cons "aloha" '()))) 5) "hello")
(define (string-with-length los desired-length)
  (cond [(empty? los) "no such string"]
        [(cons? los)
         (if (= (string-length (first los)) desired-length)
             (first los)
             (string-with-length (rest los) desired-length))]))
Design the function find-first-match, which abstracts these two functions. Be sure to redefine matching-x-posn and string-with-length using your abstraction.

Exercise 3 Consider the following data definitions:
(define-struct pair [x y])
 
; A StringPairing is a (make-pair String String)
 
; A NumBoolPairing is a (make-pair Number Boolean)
Design data which abstracts these two definitions. Be sure to redefine StringPairing and NumBoolPairing using your abstraction.

Understanding Abstractions

Goals: Understand the signatures and purposes of each pre-defined list abstraction.

Exercise 4 Determine the signature of the following function:
(define (f x y z)
  (or (andmap string-numeric? (map x (filter positive? (foldr append '() y)))) z))

Exercise 5 For each of the following tasks, think carefully about what it is asking you to do. Then determine the best pre-defined list abstraction to use, to implement this task. Have a staff member look over your answers before you continue.
  • Create a list of the string-lengths of every string in a list.

  • Determine whether any Posn in a list is within a certain area.

  • Given a list of strings, create a list of only the strings that contain only numbers.

  • Check whether every number in a list is within a certain range.

  • Given a list of Posns, create a single Posn whose x-coordinate is the sum of all the x-coordinates in the list and whose y-coordinate is the sum of all the y-coordinates in the list.

Exercise 6 What is the difference between abstracting two functions and using an abstraction?

Exercise 7 Design the function double-in-list which, given a list of Strings, returns a list with all the same strings but where each instance of the word "double" appears twice in a row. So, for example, given the list

(list "a" "double" "c" "double")

the function would return:

(list "a" "double" "double" "c" "double" "double")

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.