Read Section 10.2 and do the following exercises:
Read Section 11.3 in the textbook and do the following exercises:
(check-expect (prefixes (list "a" "b" "c")) (list empty (list "a") (list "a" "b") (list "a" "b" "c")))
(check-expect (prefixes (list "a")) (list empty (list "a")))
;; base case:
(check-expect (prefixes empty) (list empty))
(check-expect (prefixes (list "a" "b" "c")) (list (list "a") (list "a" "b") (list "a" "b" "c")))
;; base case:
(check-expect (prefixes (list "a")) (list (list "a")))
Don't forget to write signatures before you write the any code!
Note that the problem also involves the suffixes. You need to develop the check-expects for it.
Write a function list-append
that takes two lists and combines them into one list:
(check-expect (list-append (list 2 5 6) (list 1 3)) (list 2 5 6 1 3))
You need more test cases (and, as always, a signature) before you write the function.
This problem has a lot in common with Problem 2, so you might want to carefully study your solution
before you attempt this one. Just like in problem 2, you will be working with lists of letters (1Strings).
Your goal is to write a function arrangements
that takes a list of letters and returns a
list of all possible rearrangments of these letters. For instance, (arrangements (list "c" "a" "r"))
would produce a list
(list (list "c" "a" "r") (list "a" "c" "r") (list "a" "r" "c") (list "c" "r" "a") (list "r" "c" "a") (list "r" "a" "c"))
(which may or may not be in this order).
You would need to use the function list-append
that you wrote in Problem 3 (or you may use an equivalent
function append
that is already defined in Racket).
The function arrangements
will require several recursive helper functions.
The discussion here
gives you some hints for working on this problem. Work at your own pace, use paper and pencil to
work through examples, and ask questions when you get stuck.
The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.