$24
For each exercise in this lab, try solving it without defineing any helper functions: use lambda only, and list abstractions wherever possible. For some problems, you might want to use local to define constants that may help.
Be sure to write out the signatures and purpose statements for every problem (especially if the problem is abstract in some way) and as always, write sufficient examples to be confident in your work.
Exercise (Reviewed) 1 Design a function that takes in a list of items, and a predicate over those items, and returns whether the predicate rejects any items in the list.
Exercise (Reviewed) 2 Design a function that takes in a Nat, and builds a list (list 0 -1 2 -3 4 ...) of that requested length, where all the odd numbers are negative and all the even ones are positive.
Exercise 3 Design a function that takes in a list of Strings and a String, and removes any copies of that string from the list. (Note: we are considering whole strings only, not substrings.)
Exercise 4 Design a function that takes in a list of numbers, and adds the sum of all the numbers in the given list of numbers to every number in the list.
Exercise 5 Design a function that takes in a Nat n, and returns a square n-by-n matrix. The values in the matrix should start at 1 in the top-left corner, and increase to the right and down:
1 2 3 4 ... n
2 3 4 5 ... n+1
3 4 5 6 ... n+2
...
n n+1 n+2... 2n-1
Exercise 6 Design a function that takes in a list of items and a list of predicates over those items, and filters out any element in the given list that are rejected by any of the predicates in the list of predicates.
Exercise 7 Design a function that takes in a list of list of booleans, and counts how many of those lists contain at least one #true value in them. Try solving this problem two different ways: once using length and once using foldr.
Exercise 8 Design a function that takes in a list of list of list of strings, and returns the total length of all the strings contained in the input. Try solving this problem two different ways: once by "finding out all the string lengths first, then summing everything after", and once using as few list abstractions as possible.
Exercise 9 Consider the following data definition of a choose-your-own-adventure story:
(define-struct story-tree [story branch-1 branch-2])
(define-struct ending [happy])
; A StoryTree is one of:
; - (make-ending Boolean)
; - (make-story-tree String Tree Tree)
Complete the template and give several examples for this data definition.
Exercise 10 Lots of choose-your-own-adventures are pretty boring to read, since most of the endings aren’t very satisfying. Design a function to compute a rating for the story, which we’ll define to be the ratio of the number of happy endings to the number of sad endings.
Exercise 11 Printed books are no longer trees; we have to print them out in order. Design a function that takes in a StoryTree, and concatenates all the strings in the tree into one long string. Use ":(" for a sad ending and ":)" for a happy one.
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.