Lab 9: Using builtin 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 redefine 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.
(sometimeshello (list "Daniel" "Alicia" "Bao" "Amina" "Hiroki"))
(list "Daniel" "Alicia" "Hello, Bao" "Hiroki" "Hello, Amina")
(list "Hello, Daniel" "Alicia" "Bao" "Hiroki" "Amina")
Set the likelihood of saying hello to someone according to your own personal preference.
Using buildlist with lambda
Exercise 4
Start by anticipating (and then determining) the values for the following expressions
(buildlist 10 (lambda(x) x)) (buildlist 10 (lambda(x) (+ 1 x))) (buildlist 10 (lambda(x) 1)) (buildlist 5 (lambda (x) (/ x 10))) (buildlist 10 (lambda (x) (expt 3 x)))
Design function evensfirst 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. 
> (evensfirst 4) 
(list 0 2 4 6) 
> (evensfirst 7) 
(list 0 2 4 6 8 10 12) 
> 
Also define evens*first that starts from 2 and oddsfirst that returns the list of the first that many odd numbers.
Exercise 5 Design a function powersoften 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. 
> (powersoften 5) 
(list 1 0.1 0.01 0.001 0.0001) 
> (powersoften 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:
(buildlist 10 (lambda (x) (if (= x 3) 1 0))) 

(buildlist 10 (lambda (x) (if (= x 4) 1 0))) 

(buildlist 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:
(buildlist 10 (lambda (x) (random 10))) (buildlist 10 (lambda (x) (random (add1 x))))
Then design a function randombetween with the following signature and purpose statement:
; Number Number Number > [ListOf Number] ; generates list of howmany numbers randomly chosen between low and high ; (define (randombetween low high howmany)
Here’s how my function works:
Welcome to DrRacket, version 6.0.1 [3m]. 
Language: Intermediate Student with lambda; memory limit: 128 MB. 
> (randombetween 50 50 10) 
(list 19 39 43 50 8 33 9 29 17 23) 
> (randombetween 100 200 5) 
(list 158 115 175 170 182) 
> 