6.6

### Lab 8 ISL Abstractions, Local, and Scope Purpose: The purpose of this lab is to practice the use of ISL list abstractions and the use of local.

Textbook References: Chapter 16.1: Existing Abstractions, Chapter 16.2: Local Definitions

### Existing Abstractions

Exercise 1 Provide a signature for the below function:
 (define (func x y z) (foldr (x y) (first z) (rest z)))

Exercise 2 Provide a signature for the below function:
 (define (help x y z) (map (first x) (filter y z)))

Please submit the above exercise to the handin server by 10pm EDT on the day of lab.

Exercise 3 Using a list abstraction, design the function repeat-num which takes a list of numbers and a number and produces a list that is the same but with the given number duplicated each time it appears. For example, given the list (list 1 2 3 2) and the number 2 your function should produce (list 1 2 2 3 2 2).

Exercise 4 Decide on the best list abstraction to use for each of the following functions. You do not need to design the functions.
• A function that takes a list of Posns and a single Posn and checks if any of the Posns in the list match the single Posn.

• A function that takes a list of Strings and returns a list of their lengths.

• A function that takes a list of numbers and checks if every number is greater than 100.

• A function that takes a list of Strings and produces only the Strings that start with "hello".

### Local and Scope

Exercise 5 What does the following code produce? Show the steps to get to the solution.
 (define (confusion x y z) (local [(define x 100)] (+ (sqr y) (local [(define y (sqrt x))] (- z y)) y))) (confusion 10 5 50)

Note: Although the above function only provides one definition in each local expression you can have more than one definition. Using nested locals is strongly discouraged and usually indicates that you need to either simplify your function or make use of more helper functions.

Exercise 6 The above function is poorly designed and overly complex. Simplify it as much as possible without changing its signature and add a signature and tests. You do not need to add a purpose statement to this function.

Exercise 7 Design the function adder which takes a number and produces a function that adds that number to its input. For example, given the number 1 your function should produce a function that is equivalent to add1. Note: in order to test this function you will have to call it on several inputs and test those functions.

### 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.

### Extra Practice

This section is here to give you some extra practice problems.

Consider the following data definition:
 (define-struct pcons [x y more]) ; A [PairList X Y] is one of: ; - "empty" ; - (make-pcons X Y [PairList X Y]) ; and represents a list of pairs

Exercise 8 Design the function pairlist-map which works like map but for PairLists. The signature, purpose, and tests are provided below.
 ; pairlist-map : [PairList X Y] [X -> A] [Y -> B] -> [PairList A B] ; Perform the given transformations on each pair in the list (check-expect (pairlist-map "empty" sub1 identity) "empty") (check-expect (pairlist-map (make-pcons "hi" 2 (make-pcons "cat" 5 "empty")) string-length add1) (make-pcons 2 3 (make-pcons 3 6 "empty")))

Exercise 9 Design the function pairlist-filter which works like filter but for PairLists. The signature, purpose, and tests are provided below.
 ; pairlist-filter : [PairList X Y] [X Y -> Boolean] -> [PairList A B] ; Produce a list of the pairs that pass the given test (check-expect (pairlist-filter "empty" <) "empty") (check-expect (pairlist-filter (make-pcons "a" "b" (make-pcons "d" "c" "empty")) string

Exercise 10 Design the function pairlist-fold which works like foldr but for PairLists. The signature, purpose, and tests are provided below.
 ; pairlist-fold : [PairList X Y] [X Y Z -> Z] Z -> Z ; Combine the pairs in the given list using the given function and base case (check-expect (pairlist-fold "empty" * 10) 10) (check-expect (pairlist-fold (make-pcons 10 5 (make-pcons 4 2 "empty")) + 0) 21)