Lab 3
Develop a representation for bicycles; A bicycle can be either a Trek, a Bianchi, or a Gunnar. Each one has a single field that is a number. I don’t care what the single field of each kind bicycle is called. Actually, I don’t care whether they have fields or not, but experience shows that constructors with no arguments along with higher-order procedures confuse the heck out of students just getting used to Racket. If this doesn’t make sense to you, just take my advice and make up a field for each one. How about num-wheels? Define these using a define-type and a set of structs.
Note that every structure you define in this course should have the #:transparent tag. There’s a note about this in the Hints linked to in the syllabus.
Note! After defining a structure named s1 with fields f1 and f2, you will be able to use the function s1? to determine whether a given value is a structure created with s1, as well as being able to use s1-f1 to get the contents of field f1 and s1-f2 to get the contents of field f2. This will be useful later....
Develop the function only-treks, that consumes a list of bicycles and returns a list containing only the Treks. Hint: it’s fine to use a match as part of the list template, but don’t use a match to determine what kind of bicycle you have; this will impair the abstraction that’s supposed to happen two problems in the future. (Also, don’t use the built-in filter function; the idea of this lab is to give you practice in writing recursive functions on lists. Unless you re-implement filter. Then it’s fine.)
Develop the function only-bianchis, that consumes a list of bicycles and returns a list containing only the Bianchis. Hint: same hint as last time.
Abstract over the two of these to obtain the function onlyThese, that consumes a list of bicycles and a particular bicycle predicatePredicate: a function that accepts any value and returns a boolean, used to represent a set or property. For instance, an even? function or Java’s isOrdered. f (e.g., Gunnar?, or maybe a function that always returns false) and returns a list of bicycles containing only those elements of the list that satisfy f. (Note: you’re passing the function Gunnar? here. If this doesn’t make sense, ask for help!
Hint: use (Listof Bicycle) as the return type of onlyThese.
Develop the my-append function that consumes two lists and returns the result of appending the second one to the first. So, if called with the list '(a b c) and the list '(d e f), it would return '(a b c d e f). (Don’t use the built-in append function, please.)
Develop the my-take function that consumes a list and a number n and returns the first n elements of the list. If the list contains fewer than n elements, it returns the entire list. (Again, don’t use take, or reverse, or drop, etc. etc.)
1 Followup Questions
These are optional. Thinking about them might help you do better on quizzes and tests. Let us know if you have questions about them, we’re happy to discuss them!
Why did we tell you not to use match in writing the only-treks function? What happens if you don’t?
Does your implementation of the my-take function call length? If so, what is the asymptotic running time of your my-take function?
How many of these functions can be implemented using for/list ?
How precise can the type for only-these be? Can you refine it so that when it’s called with Gunnar? that it returns the type (Listof Gunnar)? And, more generally, a type such that when called with a predicate that only returns true for type U, that the result of the call has type (Listof U) ? BTW, this is a VERY HARD question.
Optional: these functions are definitely NOT in scope for Quiz 3... How many of these functions can be implemented using map, filter or foldl ?