Lab 5 Lists
Purpose: The purpose of this lab is to you some hands-on experience with lists.
Textbook references: Chapter 9: Designing with Self-Referential Data Definitions
Warmup
Exercise (Reviewed) 1 Design a data definition for lists of numbers.
Exercise (Reviewed) 2 Design a function any-negatives? that determines if any element in a list of numbers is negative.
Exercise 3 Design a function mean which computes the mean of a list of numbers. Recall that a mean of a group of numbers is its sum divided by its size. Be sure to write a check-error for when the length of the list is 0.
Exercise 4 Design a function which only keeps the even numbers in a list of numbers.
Exercise 5 Design a function which adds 42 to every element in a listof numbers.
Exercise 6 Design a function intersection which takes in two list of numbers and returns the list of elements appearing in both. member? will be helpful.
Next Slide, Please
Goals: Design a program with lists.
We are going to design a simple slide show program. Every slide will have a title and a list of bullets to display, and each slide and item will be preceded by an event.
; A Slideshow is one of: ; - '() ; - (cons Slide Slideshow) ; and represents an ordered slideshow ; A Slide is a (make-slide String LoS LoS) (define-struct slide [title shown hidden]) ; and represents a slide's title, what bullets have been shown, and which are hidden ; An LoS is one of: ; - '() ; - (cons String LoS) (define SLIDE-1-LOS (cons "Value systems are neat." (cons "They help us determine the best ways to live." (cons "We each have one." (cons "But what value system is best?" '()))))) (define SLIDE-1 (make-slide "Value Systems" '() SLIDE-1-LOS)) (define SLIDE-2-LOS (cons "Picking a best value system requires comparing them." (cons "Comparison requires measurement." (cons "Sample measurement: which value systems minimizes suffering." (cons "But this presents a problem..." '()))))) (define SLIDE-2 (make-slide "The Best Value Systems" '() SLIDE-2-LOS)) (define SLIDE-3-LOS (cons "The minimization of suffering is a value choice." (cons "Another one would be maximizing happiness, for example." (cons "Any value system measurement is a value system itself." (cons "Thus, the best value system cannot ever be determined." '()))))) (define SLIDE-3 (make-slide "Value Systems: Axiomatic" '() SLIDE-3-LOS)) (define SLIDESHOW (cons SLIDE-1 (cons SLIDE-2 (cons SLIDE-3 '())))) ; slideshow-temp : Slideshow -> ? (define (slideshow-temp slideshow) (cond [(empty? slideshow) ...] [(cons? slideshow) (... (slide-temp (first slideshow)) (slideshow-temp (rest slideshow)))])) ; slide-temp : Slide -> ? (define (slide-temp slide) (... (slide-title slide) (los-temp (slide-shown slide)) (los-temp (slide-hidden slide)))) ; los-temp : LoS -> ? (define (los-temp los) (cond [(empty? los) ...] [(cons? los) (... (first los) (los-temp (rest los)))]))
Exercise 7 Design the function draw-slide which draws a slide, showing only its title and unhidden content on a large background of a fixed size. The text of the bullets should be arranged above each other.
Exercise 8 Design the function draw-slideshow which draws either the slideshow’s first slide or the text "Fin.", which is always elegant. This should be placed on a large background of a fixed size.
Exercise 9 Design the function advance-slide which moves the first entry in a slide’s hidden content to the end of its shown content if there is any hidden content. Hint: append.
Exercise 10 Design the function slide-over? which determines if a slide is over (none of its bullets are hidden).
Exercise 11 Design the function advance-slideshow which either advances its first slide if it has more content to be shown or moves onto the next slide if there is one.
Exercise 12 Design a big-bang program which will advance a slideshow by either a click, a "right" arrow press, or once every two seconds (up to you). The program should end when there are no more slides left.
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.
Bullet Hell
Goals: Practice using lists inside structures and structures inside lists.
Exercise 13 Design a function which determines whether or not a given string appears anywhere inside a slideshow. Hint: string-contains?.
Exercise 14 Design a function which determines the total number of characters in a slideshow.