Purpose: This lab will give you practice using existing list abstractions.
Exercise 1 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 2 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)
Goals: Understand the signatures and purposes of each pre-defined list abstraction.
Exercise 3 Determine the signature of the following function:
Exercise 4 Determine the best pre-defined list abstraction to use for each of the following tasks. 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 5 What is the difference between abstracting two functions and using an abstraction?
Exercise 6 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 "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").
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.