Question 1: Lists? We don’t need no stinking lists!:
Recall that we defined a (listof ™) as follows:
; A (listof T) is one of:
* (cons I (listof T))
Copyright By PowCoder代写 加微信 powcoder
We then used the built-in functions first and rest to extract parts of the list, and the built-in function cons to make a list one longer.
Here we will create a structure that is able to store arbitrarily long data, just like a list. For simplicity, instead of working with any type , we will work only with Int.
For this question, use the following data definition:
1 (define-struct ls (first rest))
a ls is one of:
(make-ls Int Ls)
a. Length. Write a function ls-length that consumes a Ls and produces the number of values in it.
For example,
(check-expect (ls-length (make-ls 5 (make-ls 2 (make-ls 11 empty)))) 3)
b. Min. Write a function ls-min that consumes a non-empty Ls and produces the smallest value.
For example,
(check-expect (ls-min (make-ls 5 (make-ls 2 (make-1s 11 empty)))) 2)
Submit your solution in the file a08q1. rkt.
Question 2: Keeping Count
Write a function (keep-counts lists mincount threshold), where lists is a (listof (listof Num)), mincount is a Nat, and threshold is a Num.
This function produces a list containing all the values in lists which contain at least mincount values greater than threshold. For example,
>(keep-counts (list
(list 1 1 2 5)
(list 1 4 4 1)
1 a d’ist 9151 7 233
– (keep-counts (list
(list 101 101 102)
(list 50 25 70)
(list 1000)
(list 1 2 3 4 5 6 7)) 1 100)
(list (list 101 101 102) (list 1000))
Do not write any recursive functions for this question; that is, do not write any function that calls itself. Solutions that use recursion will receive a mark of o for correctness.
Submit your solution in the file a08q2.rkt.
Question 3: A Strange Recursive Structure
We can store information in many different ways. With lists, we store a first item, and the rest of the items
Here we will store the contents of a Str, in three parts:
1. the first character
2. the middle characters
3. the last character.
For example, in “Babbage” the parts are B, abbag, and e.
If we store the middle characters using the same kind of structure, we have a recursive data structure that stores the contents of a Str.
We will use the following data definition:
1 (define-struct silly-string (first middle last))
a SillyStr is one of:
* a (make-silly-string Char SillyStr Char)
a. Sillify. Write a function (sillify s) that consumes a Str and produces the corresponding SillyStr.
For example,
1 (check-expect (sillify
“”) empty)
(check-expect (sillify
(check-expect (sillify “yo”) (make-silly-string #\y empty #\o))
(check-expect
“Babbage”)
(make-silly-string # \B
(make-silly-string #la
(make-silly-string #\b
b. Palindromes
A palindrome is a sequence of characters that is the same when read forwards as backwards: for example, “racecar” is a palindrome, while “Koenigsegg” is not. If the first and last characters
are equal, and the middle characters are a palindrome, then the sequence is a palindrome. This corresponds to the structure of a SillyStr, so our SillyStr data structure is ideally
designed for identifying palindromes. (We will not consider lower- and upper-case letters to be the same. So we will not consider “Hannah” to be a palindrome, even though “hannah” is.)
Write a function (palindrome? ss). It consumes a SillyStr and produces true if ss is a palindrome, and false otherwise. For example,
1 (check-expect (palindrome? (make-silly-string #h
(make-silly-string #e #ih#l
(check-expect (palindrome? (make-silly-string #\r
(make-silly-string #\a #d #la)
(check-expect (palindrome? (sillify
“racecar”)) true)
10 (check-expect (palindrome? (sillify
“heptapod”)) false)
(check-expect (palindrome? (sillify
“able was I ere I saw elba”)) true)
12 (check-expect (palindrome? (sillify
“Hannah”)) false)
13 (check-expect (palindrome? (sillify
“hannah”)) true)
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com