1.Write the function (my-length L) that counts the number of
a data expressions in list L. Use a recursive process.
Test Cases:
(my-length '()) -> 0
(my-length '(a b c)) -> 3
(my-length '(a (b c)) -> 2
a. ;; List -> Integer
(define my-length
(λ(L)
b. Which process does this function use?
c. Draw a trace diagram for (mylength '(a b c)).
d. The function (length L) is a built in function.
2. Assume L is a list. Write the function (num-atoms L) that counts
the number of items in L that are atoms.
Test Cases:
(num-atoms '()) -> 0
(num-atoms '(a b (c d))) -> 2
(num-atoms '((1) (2 3)) -> 0
;; Data -> Boolean
(define atom?
(λ(x)
(not (list? x))))
;; List -> Integer
(define num-atoms
(λ(L)
3. Assume L is either empty or is a list of numbers. Write (sum L)
that computes the sum of the numbers in list L.
Test Cases:
(sum '() ) -> 0
(sum '(1 2 3)) -> 6
(define sum
(λ(L)
4. Assume L is a list of numbers. Write the function (avg L)
that evaluates to the avg of the numbers in list L.
Test Cases:
(avg '(1)) -> 1
(avg '(1 2)) -> 1.5
(avg '(100 90 80) ) -> 90
5. Asume L is a list of numbers with at least 1 number in it.
Write the function (max-list L) that returns the maximum value
in L.
Test Cases:
(max-list '(100)) -> 100
(max-list '(0 3 2 5 4)) -> 5
6. Assume L is a list with at least 1 data expression in it.
Write the function (last L) that returns the last data expression.
Test Cases:
===========
(last '(3)) -> 3
(last '(1 2 3 4 5)) -> 5
i. Which type of process does this function use?
7. Assume L is a list whose length is greater than 0 and n is an
integer such that 0= < n < (length L). Write the function
(item n L) that returns the data item found at index n in list L.
Test Cases:
(item 0 '(a b )) -> 'a
(item 1 '(a b )) -> 'b
(item 2 '(a b (c d ) e)) -> '(c d)
(item 3 '(a b (c d) e)) -> 'e
8. Write the function (reduce op L) that takes a function/operator op
and a list L. Assume the data expressions of L are appropriate arguments
for op. (reduce op L) reduces the list L by applying op to the data expressions
in L read from left to right.
example:
(reduce + '(1 2 3)) = ((1 + 2) + 3) = 6
(reduce max '(1 2 3)) = max(1,max(2,3)) = 3
(reduce min '(1 2 3)) = min (1,min(2,3)) = 1
(reduce * '(1 2 3)) = ((1 * 2) * 3) = 6
(reduce - '(1 2 3 4)) = (((1 - 2) - 3) - 4) = -8
The (reduce op L) function should mimic the built in function (apply op L).
(apply + '(1 2 3)) = 6