On this page:
Understanding Local and Scope
Using Local with Abstractions
Before You Go...
7.9

Lab 7 Scopes

lab!

Purpose: The purpose of this lab is to practice local and scope.

Textbook References: Chapter 16.2: Local Definitions, Intermezzo: Scope

Understanding Local and Scope

Exercise 1 Complete the following check-expects with the expected value. Run them to double check your answers. Hint: a good first step would be to provide the signatures for the given functions (a popular exam question).

(define (f x y z)
  (local [(define x (+ y z))]
    (local [(define y z)]
      (local [(define z (* x y))]
        (+ x y z)))))
 
(check-expect (f 1 2 3) __)
 
(define (g a b)
  (+ (local [(define b (string->number a))]
       (local [(define a (sqr b))]
         (- a b)))
     (string-length a)
     (if b 10 5)))
 
(check-expect (g "5" true) __)

Using Local with Abstractions

You should use pre-defined list abstractions to complete each of the following exercises. Some exercises may need more than one abstraction.

Exercise 2 Design a function which, given a list of numbers and a value n, multiplies each number in the list by a given value if the number is positive—otherwise keeps the number as is—and sums all numbers so obtained. For this exercise, you should use only one list abstraction.

Hint: Your helper function must be local. What would happen if you tried this with an external helper?

Exercise 3 Design the function numlist-intersect, which takes two lists of numbers l1 and l2 and returns the list of numbers contained in both l1 and l2. For this exercise, duplicate elements in the output are fine.

Remember that the goal is to use list abstractions—this makes this problem a bit tricky (and thus interesting). Exploit the power of local.

Also, this function takes two lists as input—we haven’t had much of that so far. What does this mean for the test cases—how many should there be, and which?

Exercise 4 Practice abstraction: the previous exercise assumes lists of numbers. Design a new function intersect that works for two [List-of X]. Slight caveat: to compute the intersection, you need to know how to compare data of type X for equality, as different data use different equality predicates. Solution: make the equality predicate an additional argument of your function!

Exercise 5 Design the function remove-all-matches which is given three inputs: a list, a single element to look for, and an equality function. It then removes any element of the list that is equal to the given element, where equality is determined by the given equality function.

Be sure to make the signature of your function as abstract as possible.

Exercise 6 Design the function add-all-matches which is given the same three inputs as the previous function. It then duplicates any element that is equal to the given element. For example:

(check-expect (add-all-matches (list 1 2 1) 1 =) (list 1 1 2 1 1))

Before You Go...

If you had trouble finishing any of the exercises in the lab or homework, or just feel like you’re struggling with any of the class material, please feel free to come to office hours and talk to a TA or tutor for additional assistance.