Lab 8 Lambda and Multiple Complex Inputs
Purpose: We first assign a new partner. We will then practice using lambda as well as processing multiple complex inputs.
Textbook References: Chapter 17: Nameless Functions, Chapter 23: Simultaneous Processing
New Partners!
Goals: Get a new lab partner. Make new friends, but keep the old.
Look on the handin server for your new partner and private message them on Teams to exchange contact information. If your partner is not here today you may try emailing them. Let your Head TA know if you do not hear from them in a timely manner.
Lambda
Exercise 1 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.
Exercise 2 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 3 [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.
Multiple Complex Inputs
Exercise 4 Design the function prizes, which takes two lists: one of names, the other of prize amounts in $, and produces a list of strings that announces who gets what. If we run out of prizes, the person gets 0.
Example:
(check-expect (prizes (list "alice" "bob" "carol") (list 100 50)) (list "alice gets $100" "bob gets $50" "carol gets $0"))
Exercise 5 Design the function pair-lists which, given two lists, produces a list of the pairs of elements in the same position. Each pair will be represented by a list of two elements. If one list runs out of elements, the remaining elements in the other list should be ignored. For example:
(check-expect (pair-lists (list 1 2) (list "a" "b" "c")) (list (list 1 "a") (list 2 "b")))
Exercise 6 Design the function prefix, which takes a list lp of prefixes (each prefix is a string) and a list l of strings. It produces a list of lists of strings. Each list of strings in the output is obtained by using one of the given prefixes and inserting them as the first element into l. For example:
(check-expect (prefix (list "p1" "p2") (list "A" "B" "C")) (list (list "p1" "A" "B" "C") (list "p2" "A" "B" "C")))
Exercise 7 Design the function cross-product which, given two lists, produces a list of all possible pairs of elements. Each pair will be represented by a list of two elements. For example:
(check-expect (cross-product (list 1 2) (list "a" "b" "c")) (list (list 1 "a") (list 1 "b") (list 1 "c") (list 2 "a") (list 2 "b") (list 2 "c"))) The ordering of the list does not matter.