CS2322

Laboratory Problem 5

Arithmetic on n-tuples

In this problem we develop a small arithemetic package which will perform arithmetic on n-tuples which have the same number of elements.

We define an n-tuple to be a list of 0 or more numbers. The numbers in an n-tuple are called components.

You should define and implement four functions which take two n-tuples having the same number of components and produce an n-tuple having that same number of components. The elements of the result are computed as the component-wise sum, difference, product or quotient of the two argument n-tuples. If the length of the n-tuple arguments is not the same, your functions should return 'length-error . What should the result be when the length of both n-tuples is zero? Call these functions

n-tuple-plus

n-tuple-minus

n-tuple-times

n-tuple-divide

For example:

(n-tuple-plus '(1 2 3) '(0 -1 4))

==>(1 1 7)

(n-tuple-times '(1 2 3) '(0 -1 4))

==>(0 -2 12)

Next, extend these definitions to accept one argument of a number and the other argument an n-tuple. The result produced by these functions should be as if the numeric argument is replaced by an n-tuple of the right length, each complonent of which is equal to the numeric argument.

For example:

(n-tuple-plus 1 '(1 2 3 4))

==>(2 3 4 5)

(n-tuple-times '(2 4 6) 3)

==>(6 12 18)

It might be useful to imbed definitions of helping functions in your definitions. For example, a useful helper might be

(replicate 5 7)

==>(7 7 7 7 7)

(replicate 0 7)

==>()

Lab 5 Scheme Code