CS 440 Midterm Exam (Take-Home)
This midterm exam consists of 7 separate exercises --- 3 on recursion and lists, 3 on higher order functions, and 1 involving an addition to an interpreter. They are described below:
Part 1: Recursion and lists
flatten-1: takes a list and "flattens" any nested lists by one level. E.g., > (flatten-1 '(a (b c) d))
'(a b c d)
> (flatten-1 '((a) (b ((c) (d))) ((e f)))) '(a b ((c) (d)) (e f))
> (flatten-1 (flatten-1 '((a) (b ((c) (d))) ((e f))))) '(a b (c) (d) e f)
> (flatten-1 (flatten-1 (flatten-1 '((a) (b ((c) (d))) ((e f)))))) '(a b c d e f)
●
riffle takes one or more lists and "riffles" (shuffles) their contents together in
alternating fashion into one single list. E.g.,
> (riffle '(a b) '(1 2 3 4) '(u v w x y z)) '(a 1 u b 2 v 3 w 4 x y z)
> (riffle (range 5) (range 6 10) (range 10 15)) '(0 6 10 1 7 11 2 8 12 3 9 13 4 14)
●
wordle takes two strings -- a solution and guess (a la Wordle) -- and returns a list of clues that indicate which characters in the guess are in the correct spot (*), which match a character from the solution but are in the incorrect spot (+), and which don't match any solution characters at all (_).
Correct-spot characters are given precedence, and incorrect-spot characters are matched from left to right. You should assume that the solution and guess are the same
length. E.g.,
> (wordle "CATCH" "PARCH") '(_ * _ * *)
> (wordle "FASTER" "STREAK") '(+ + + + + _)
> (wordle "SWEETLY" "TWENTYS") '(_ * * _ * + +)
●
You may find it useful to use the string->list function, which takes a string and returns a list of characters (which you can compare using eq?). You may also find for (or a variant) helpful, though not necessary.