3.3 Using High-Level Functions

Answers to Exercises

Problem 1

•• Using the functions presented in this section, along with arithmetic, give a one-line rex definitions of the following function:

infix(I, J, L) yields elements I through J of list L, where the first element is counted as element 0. For example,

rex > infix(1, 5, range(0, 10));
[1, 2, 3, 4, 5]

Create the function infix by first clipping off the unnecessary values at the end of list L using the antiprefix function and inputted value J. Then by using the prefix function with inputted values I and J you can clip off the unnecessary values at the beginning of list L, leaving only the specified range of values in between.

rex> infix(I,J,L)=prefix(J-I+1,antiprefix(I,L));

Problem 2

•• An identity on two functional expressions indicates that the two sides of the expression are equal for all arguments on which one of the two sides is defined. Which of the following identities are correct?

append(L, append(M, N)) == append(append(L, M), N)
Correct

reverse(reverse(L)) == L
Correct

reverse(append(L, M)) == append(reverse(L), reverse(M))
Incorrect

sort(append(L, M)) == append(sort(L), sort(M))
Incorrect

reverse(sort(L)) == reverse(L)
Incorrect

sort(reverse(L)) == sort(L)
Correct

[A | append(L, M)] == append([A | L], M)
Correct

reverse([A | L]) == [A | reverse(L)]
Incorrect

reverse([A | L]) == [reverse(L) | A]
Incorrect

reverse([A | L]) == append(reverse(L), [A])
Correct

Problem 3

••• Two functions of a single argument are said to commute provided that for every argument value X, we have f(g(X)) == g(f(X)). Which pairs of functions commute?

sort and remove_duplicates
Commutes

reverse and remove_duplicates
Does not commute

sort and reverse
Does not commute


Return