On this page:
1 Map and Fold
Using build-list with lambda
6.10.1

Lab 9: Using built-in abstractions

Note: Use the "Intermediate Student with lambda" language on this and further assignments in this course.

Note: Whenever you abstract from concrete things, remember to re-define them using the new abstraction. (You don’t need to submit the original versions.) They should have the same signatures and purpose statements as before as well as pass the same tests as before. (Helpers are often useful for eliminating inessential differences among concrete things.) The new abstraction should have its own signature and purpose statement. If the original tests do not provide sufficient coverage for the abstraction, include additional tests.

1 Map and Fold

Exercise 1 Design a function farthest which accepts a posn and list of posn and uses either foldl or foldr to find the posn farthest from the given posn. You should use local to define a local helper function.

Exercise 2 Write a function count which accepts a list of numbers and uses either foldl or foldr to count the number of items in the list. You may NOT use the built in length function in this exercise. You will have to write an auxiliary function, which is given to the foldl or foldr function, which will perform the counting.

Test the function with a few lists of numbers.

Now abstract this function to work with a list of any data type.

Exercise 3 Write a function sometimes-hello which randomly says hello to some of the people whose names appear in the input list of strings but not to others. Use map. For example,
(sometimes-hello
  (list "Daniel" "Alicia" "Bao" "Amina" "Hiroki"))
might yield
(list "Daniel" "Alicia" "Hello, Bao" "Hiroki" "Hello, Amina")
or
(list "Hello, Daniel" "Alicia" "Bao" "Hiroki" "Amina")

Set the likelihood of saying hello to someone according to your own personal preference.

Using build-list with lambda

Exercise 4

Start by anticipating (and then determining) the values for the following expressions

(build-list 10 (lambda(x) x))
 
(build-list 10 (lambda(x) (+ 1 x)))
 
(build-list 10 (lambda(x) 1))
 
(build-list 5 (lambda (x) (/ x 10)))
 
(build-list 10 (lambda (x) (expt 3 x)))

Design function evens-first of one argument, a natural number, that returns the list of the first that many even numbers starting with 0. Here’s how my function works:

Welcome to DrRacket, version 6.0.1 [3m].

Language: Intermediate Student with lambda; memory limit: 128 MB.

> (evens-first 4)

(list 0 2 4 6)

> (evens-first 7)

(list 0 2 4 6 8 10 12)

>

Also define evens*-first that starts from 2 and odds-first that returns the list of the first that many odd numbers.

Exercise 5 Design a function powers-of-ten of one argument, a natural number, that produces the powers of 10 in descending order by one, starting from exponent 0 (zero).

Welcome to DrRacket, version 6.0.1 [3m].

Language: Intermediate Student with lambda; memory limit: 128 MB.

> (powers-of-ten 5)

(list 1 0.1 0.01 0.001 0.0001)

> (powers-of-ten 10)

(list 1 0.1 0.01 0.001 0.0001 0.00001 0.000001 0.0000001 0.00000001 0.000000001)

>

Exercise 6 Start by anticipating the values for the following expressions:

(build-list 10 (lambda (x) (if (= x 3) 1 0)))

 

(build-list 10 (lambda (x) (if (= x 4) 1 0)))

 

(build-list 10 (lambda (x) (if (= x 5) 1 0)))

Then design a function diagonal that receives one argument, a number, and returns a list of that many lists of 0 and 1 in a diagonal arrangement. Here’s how my function works:

Welcome to DrRacket, version 6.0.1 [3m].

Language: Intermediate Student with lambda; memory limit: 128 MB.

> (diagonal 3)

(list (list 1 0 0) (list 0 1 0) (list 0 0 1))

> (diagonal 10)

(list

 (list 1 0 0 0 0 0 0 0 0 0)

 (list 0 1 0 0 0 0 0 0 0 0)

 (list 0 0 1 0 0 0 0 0 0 0)

 (list 0 0 0 1 0 0 0 0 0 0)

 (list 0 0 0 0 1 0 0 0 0 0)

 (list 0 0 0 0 0 1 0 0 0 0)

 (list 0 0 0 0 0 0 1 0 0 0)

 (list 0 0 0 0 0 0 0 1 0 0)

 (list 0 0 0 0 0 0 0 0 1 0)

 (list 0 0 0 0 0 0 0 0 0 1))

>

Exercise 7 Start by anticipating the values for the following expressions:

(build-list 10 (lambda (x) (random 10)))
 
(build-list 10 (lambda (x) (random (add1 x))))

Then design a function random-between with the following signature and purpose statement:

; Number Number Number -> [ListOf Number]
; generates list of how-many numbers randomly chosen between low and high
; (define (random-between low high how-many)

Here’s how my function works:

Welcome to DrRacket, version 6.0.1 [3m].

Language: Intermediate Student with lambda; memory limit: 128 MB.

> (random-between -50 50 10)

(list -19 -39 -43 -50 -8 33 -9 -29 17 23)

> (random-between 100 200 5)

(list 158 115 175 170 182)

>