On this page:
A List of Something
Operating on List Elements
Removing Some Elements
Combining Elements
6.10

Lab 15: [Listof Anything]

Implement this lab with the Intermediate Student Language.

Make sure you follow The Style we use for the {B,I,A}SL{,+} languages in this class.

Choose the initial Head and Hands, and get started!

A List of Something

We’ve seen the parameterized data definition for lists in class.

; A [Listof X] is one of:
; - '()
; - (cons X [Listof X])
 
; list-template : [Listof X] -> ???
(define (list-template l)
  (cond [(empty? l) ...]
        [else (... (first l)
                   ...
                   (list-template (rest l))
                   ...)]))

We can write [Listof X] (where X is any other data definition) in our signatures.

Ex 1: Write down the signature for a function foo that consumes a list of numbers and returns a list of strings.

Ex 2: Write down the signature for a function bar that consumes a list of lists of strings and returns a list of images.

Ex 3: Write down the signature for a function baz that consumes a number and returns a list of lists of lists of strings.

Ex 4: The general list data definition has a single parameter X. Write down the general data definition for Posns with two parameters X and Y.

Ex 5: Write down the signature for a function bazoinks that consumes a list of posns each containing a string and an image, and returns a posn containing two lists of numbers.

In the following exercises, be sure to use one of the higher-order functions filter, foldr, or map in your implementation. It’s always possible to use more than one, so pick the simplest one for the task at hand. Also, feel free to use local definitions for any simple helper-functions you implement, especially when they’re used in only one function.

Operating on List Elements

Ex 6: Design a function fake-news that, given a [Listof String], returns a new [Listof String] where each String in the original is replaced with the string "Fake News".

Ex 7: Design a function emphatic that, given a [Listof String], returns a new [Listof String] where each string in the original has had an exclamation point "!" added to the end.

Ex 8: Design a function auto-grader that, given a list of strings returns a list of numbers where each number is a random value between 0 and 100.

Ex 9: Design a function counts that, given a [Listof [Listof String]], returns a [Listof Number] where each number is the length of each list of strings.

Ex 10: Design a function run-tests that, given a list of numbers and a function f with the signature (Number -> Boolean), returns a list of booleans where each boolean is the result of applying f to each number in the list.

Removing Some Elements

Ex 11: Design a function only-passing that, given a list of numbers, returns a new list of numbers where any numbers in that list that are less than 66 are removed.

Ex 12: Design a function wider-than-n that, given a list of images and a natural number n, returns a new list of images that contains only the images in the original list that have an image-width greater than n.

Ex 13: Design a function only-in-bounds that, given a list of [Posn Int Int]s and four integers xmin, ymin, xmax and ymax, that returns a new list of posns that only contains posns from the original list with x values where xmin <= x <= xmax and y values where ymin <= y <= ymax.

Ex 14: Design a function shorter-than-n that, given a list of lists of any type and a natural number n, returns a new list of lists of the same type that only contains the lists in the given lists that have a length less than n.

Combining Elements

Ex 15: Design a function all-true that returns #true only if the elements of the given list of booleans are all #true.

Ex 16: Design a function any-true that returns #true only if the any of the elements of the given list of booleans is #true.

Ex 17: What purpose statement would you give the following function?

; wat : [Listof X] -> [Listof X]
; ???
(define (wat l) (foldr cons '() l))

Ex 18: Design a function duplicate, that given a list of any type, returns a new list of the same type where each of the elements has been duplicated.

(check-expect (duplicate '()) '())
(check-expect (duplicate '(1 2 3)) '(1 1 2 2 3 3))
(check-expect (duplicate '((1 2 3) () (4) (5 6)))
              '((1 2 3) (1 2 3) () () (4) (4) (5 6) (5 6)))

Ex 19: Design a function prepend-all, that given a list of strings and a string delim, returns a new list of strings where each of the elements in the original has had delim placed before it.

(check-expect (prepend-all '() "\n") '())
(check-expect (prepend-all '("foo" "bar") " ") '(" " "foo" " " "bar"))
(check-expect (prepend-all '("a1" "a2" "a3") ",") '("," "a1" "," "a2" "," "a3"))