On this page:
Lists
Similarities Everywhere
Before You Go...
6.6

Lab 6 Lists

lab!

Purpose: The purpose of this lab is to practice working with lists.

Textbook references: Chapter 8: Lists, Chapter 9: Designing with Self-Referential Data Definitions

Lists

Exercise 1 John B is keeping a list of all the clues he’s found on his quest to find his father. Design the function all-big-clues? which takes a list of strings (representing the clues) and produces #t if every clue in the list is at least 10 characters long. Be sure to simplify your function as much as possible! As a reminder, here is the data definition for a list of Strings:
; A ListOfStrings is one of:
; - empty
; - (cons String ListOfStrings)

Exercise 2 Design the function clue-found? which takes a list of strings (representing the clues) and a String and returns true if the given string is found in the given list. Be sure to simplify your function as much as possible!

Please submit the above exercise to the handin server by 10pm EDT on the day of lab.

It turns out just keeping text is not providing John B with very good records. To help, he has developed a better data definition for clues:
(define-struct date [month day])
; A Date is a (make-date String PosInt)
; - where 'month' is the month of the year
; - and 'day' is the day of the month
; NOTE: This data definition is not as restrictive as it should be
; but we have simplified for the sake of time
 
(define DATE-5-30 (make-date "May" 30))
(define DATE-6-2 (make-date "June" 2))
 
(define-struct clue [what where when])
; A Clue is a (make-clue String String Date)
; - where 'what' is what the clue is
; - 'where' is where it was found
; - and 'when' is a description of when it was found
 
(define CLUE1 (make-clue "Compass" "Marsh" DATE-5-30))
(define CLUE2 (make-clue "Redfield" "Office" DATE-6-2))
 
; A ListOfClues is one of:
; - empty
; - (cons Clue ListOfClues)
; and represents a list of the clues John B has found so far
 
(define LOC-EMPTY empty)
(define LOC-FULL (cons CLUE1 (cons CLUE2 empty)))

Exercise 3 Write the templates for the above definitions.

Exercise 4 Design the function found-in-month which takes a list of clues and a String representing the month of the year and produces a list of the clues found during that month.

Exercise 5 Design the function clue-thread that takes a list of clues and produces a list of what they are with arrows between them leading to "TREASURE!".Given the empty list we will just assume he already found the treasure and produce "TREASURE!". Given LOC-FULL your function should produce "Compass->Redfield->TREASURE!".

Exercise 6 Design the function big-clues which takes a list of clues and a number and produces a list of all the clues with a ’what’ field larger than the given number of characters. For example, given LOC-FULL and 6 your function should produce LOC-FULL but given LOC-FULL and 7 your function should produce (cons CLUE2 empty).

Similarities Everywhere

Exercise 7 Consider your design of the functions found-in-month and big-clues. Design a function good-clues which abstracts these two functions. Be sure to re-define found-in-month and big-clues using your abstraction.

Exercise 8 Consider the following function which produces a string giving the locations of all the clues:
; all-locations : ListOfClues -> String
; Produces a string describing the locations of all the clues
(check-expect (all-locations LOC-EMPTY) "")
(check-expect (all-locations LOC-FULL) "Marsh Office ")
(define (all-locations loc)
  (cond [(empty? loc) ""]
        [(cons? loc)
         (string-append (clue-where (first loc)) " "
                        (all-locations (rest loc)))]))

Design the function append-clues which abstracts your clue-thread function and the all-locations function shown above. Be sure to re-define clue-thread and all-locations using your abstraction.

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.