(require 2htdp/universe) (require 2htdp/image) ; ; Problem 1: ; ; Given the following define-struct statement, name all the func- ; tions that are created when this line is executed, give ; their contracts, and identify them as constructor, accessor, ; mutator, and type checker. ; ; (define-struct animal (family genus species)) ; ; family genus and species are strings. ; ; ; Problem 2 ; ; Draw the binary search tree that results from inserting ; the following numbers (as btnodes), in the order given, ; into an initially empty binary search tree: ; 55, 25, 89, 77, 65, 12, 5, 14, 90. ; ; Give the numbers contained in each of the following nodes: ; ; The root of the BST- ; ; The internal nodes of the BST- ; ; The leaves of the BST- ; ; Problem 3 ; ; Write the function PREFIX-SUM that consumes a list of numbers ; and produces a list of numbers in which every number except ; the first is replaced by the sum of the previous number (which ; may itself be a sum) in the list added to its value. ; ; The first number on the list should be added to the output ; list unchanged. ; ; Examples to help clarify the problem: ; ; > (prefix-sum '(9 2 4 2 8)) ==> (9 11 15 17 25) ; ; > (prefix-sum '(1 2 3 4 5 6 7)) ==> (1 3 6 10 15 21 28) ; ; > (prefix-sum '(1 2 3 4)) ==> (1 3 6 10) ; ; > (prefix-sum '(1 2)) ==> (1 3) ; ; > (prefix-sum '()) ==> () ; ; > (prefix-sum '(5)) ==> (5) ; ; ; ; Problem 4: ; ; Develop the function swap-posn, which consumes a posn structure ; and swaps the values in the two fields. ; ; First, write a non-mutating function called swap-posn and then ; write a mutating function called swap-posn!. ; ; ; Problem 5: ; ; Develop the function double-all-ohs which consumes a string ; and adds an extra o for each lowercase or uppercase "o" in the ; string. ; ; Examples to help clarify the problem: ; ; > (double-all-ohs "stop") ==> "stoop" ; ; > (double-all-ohs "Onward!") ==> "OOnward!" ; ; > (double-all-ohs "") ==> "" ; ; > (double-all-ohs "hello world") ==> "helloo woorld" ; ; > (double-all-ohs "cat") ==> "cat" ; ; ; Problem 6 ; ; Write the function COUNT-WORDS that consumes a phrase ; (a string) and returns the number of words in the given ; string. You can assume that words are separated by any non- ; alphabetic characters. ; ; > (count-words "This is a sentence.") => 4 ; ; > (count-words "a b c d e f g h i") => 9 ; ; > (count-words "") => 0 ; ; Hint (IF YOU ARE HAVING PROBLEMS WRITING THIS ; FUNCTION): Use the char-alphabetic? and substring ; functions and an external helper function to search ; for the position of the next non-alphabetic char in ; the input string. ; ; ; ; Problem 7 ; ; Write the function LIST-WORDS that consumes a phrase ; (a string) and returns each word in the given string as ; a list of strings. ; ; You can assume that words are separated by any non- ; alphabetic characters. ; ; > (list-words "This is a sentence.") => ; ("This" "is" "a" "sentence") ; > (list-words "") => () ; ; > (list-words "Four score and seven years ago") => ; ("Four" "score" "and" "seven" "years" "ago") ; ; Hint: You may be able to re-use an external helper ; function you wrote for problem 8 in the solution for this ; problem. ; ; ; Problem 8: ; ; Define a function copy-n that consumes a list of anything and a ; number, N, and produces the contents of the input list appended ; to itself N times. ; ; Examples to help clarify the problem: ; ; > (copy-n (list 1 2 3) 4) ==> (list 1 2 3 1 2 3 1 2 3 1 2 3) ; ; > (copy-n (list 1 2 3) 0) ==> empty ; ; > (copy-n empty 4) ==> empty ; ; ; Problem 9: ; ; Use build-list ; ; 1. To create the lists (list 0 ... 3) and (list 1 ... 4). ; ; 2. To define a function evens, which consumes a natural ; number n and creates the list of the first n even numbers. ; ; Hint: Use un-named lambda style functions in your calls to ; build-list. ; ; ; Problem 10: ; ; Use map to create the function move-all, which consumes a list ; of posn structures and translates each by adding 3 to the x- ; field. ; ; ; Problem 11: ; ; Use filter to create the function selection, which consumes 2 ; lists of quoted symbols and produces all the elements from the ; second list that are also on the first. ; ; ; ; Problem 12: ; ; Suppose you wanted to write a big-bang simulation that places an ; image of a solid maroon circle of radius 20 at the point where ; the mouse is clicked on the scene (i.e., a "button-down" event). ; At any point in time, there will be at most one circle displayed. ; ; (a) What constants would you need to define? ; ; (b) What values or structure would you use to define the state of ; the world and why? ; ; (c) Which clauses (on-draw, on-tick, on-key, on-mouse) would you ; need to include in the call to big bang? ; ; (d) Write the function used as input to each of the clauses you ; mentioned in part (c). Write the invocation of big-bang. ; ; ; Problem 13: ; ; Evaluate the result of the following expressions: ; ; (a) (filter cons? '(1 (2 3) 4 9 (8 7) ((2)))) ; ; (b) (map expt '(1 3 4) '(2 2 2)) ; ; (c) (apply + (map expt '(1 3 4) '(2 2 2))) ; ;