Lab 8 Local and Lambda
Purpose: The purpose of this lab is to practice more with local and scope, as well as how to use lambda.
Textbook References: Chapter 16.2: Local Definitions, Intermezzo: Scope, Chapter 17: Nameless Functions
More Practice with Local and List Abstractions
For the functions in this section, use local to define any needed helper functions; do not use lambda.
Exercise 1 Design a function that takes in a list of items, and a predicate over those items, and returns whether the predicate rejects any items in the list. i.e., the function should return #true if and only if the predicate returns #false for one or more of the items in the list. Make sure you write an appropriate signature, as well as sufficiently varied tests.
Exercise 2 Design a function that takes in a NaturalNumber, and builds a list (list 0 -1 2 -3 4 ...) of that requested length, where all the odd numbers are negative and all the even ones are positive.
Exercise 3 Design a function that takes in two lists of Strings, and returns a list of the shorter string for each corresponding pair across the two lists. You are guaranteed that they two lists are of the same length. For example, if you called your function with (list "aaa" "b" "ccc") and (list "a" "bbb" "c"), it should return (list "a" "b" "c").
HINT: You can actually call the map list abstraction with multiple lists! Look at the documentation for map to see what it does when you call it that way.
Lambda
Exercise 4 Design data to represent a SelfDrivingCar. A self-driving car has a make, a model, a color, and a function which shows how the car moves over time. The function should take one input (the current time) and produce a Posn representing the position of the car at that time. You must use lambda to define these functions. Be creative with your examples: create different, interesting functions for each of your SelfDrivingCars; for example, one car might travel vertically very slowly, while another car might travel more quickly in a horizontal direction.
Exercise 5 Design a function show-all-cars which takes a list of SelfDrivingCars and a time (a Number) and draw all the cars in the list at the correct position for the given time. The cars should be drawn on a background of some kind (you can pick one that you like). You can draw each car as a rectangle to avoid having to actually draw cars.
Exercise 6 [a tricky one!] Determine the most general signature of function my-function:
(define (my-function x y z) (lambda (x) (map (y z) x))) Hints:
(i) Consider renaming some variables in the definition of my-function (without changing the meaning of the definition, of course), so as to avoid confusing shadowing of other variables. This will make the definition easier to read.
(ii) Consider forming the most general signature of variables even if they do not occur as arguments of my-function. This will help you "sort out" how all variables in the entire definition hang together.