|
CMPT 120: Lab 9
This lab involves writing various recursive functions. Keep in mind the two basic parts of every recursive function, the recursion, where the problem is broken into a subproblem and recurses, and the base case, there the function can return without need of calling itself.
Write a function that takes two inputs, x and p. The function will return the value x raised to the power p, xp. Assume that p is a non-negative integer. There are a couple ways to write this recursively. The simple one is to think of xp as being equal to x * xp-1.
Write a recursive function that sums the elements of a list. Think about how the recursion worked in the example we looked at to reverse the elements of a list.
Write a recursive function that is given a list and a number and returns True if the list contains the number and False otherwise.
Once it is working, extend the function to return the index where the number was located, -1 if it wasn't found.
Chris Schmidt, last updated June 19, 2007 |