Dana Vrajitoru
C311 Programming Languages

C311 Homework 9

Due Date: Wednesday, March 15, 2023.

Ex. 1 Deep Recursion

Write a deep-recursive search function for a value in a list that is possibly nested. Use a catch-throw to exit the function when the value is found. You can combine the functions discussed in class and found in the PowerPoint presentations.

Hint: You will need to write a driver function and a recursive function. The driver function should contain the catch expression and call the recursive function inside it. The recursive function would call throw function.

Ex. 2 Dynamic Programming

Consider the two versions of the function computing the combinations as shown in the class notes. Copy them into emacs and test them. Then add a global variable counter and increment it by one at the top of both functions. This will count the number of function calls required to complete the calculations in each case. Try the two versions of the function for a few pairs of numbers n and m and print out the value of the counter after each of them. Don't forget to reset the counter to 0 before each call. Comment on the observed difference between the numbers of function calls.

Ex. 3 Dynamic Programming

Write a simple recursive function to compute the f(n) for the following sequence:

f(n) = f(n-1) + f(n-3)
f(0) = 0,
f(1) = f(2) = 1

Optimize this function using dynamic programming. You can assume that the number n in the function call for testing will be less than 20.

Ex. 4. Functions with Unlimited Number of Parameters

Write a function sum-numbers that receives an unlimited number of parameters and computes the sum of all of them that are numbers (some may not be). The non-numerical elements should be ignored. If none of the arguments is a number, the function should return 0. You will have to use the appropriate type-checking predicate here and the syntax &rest L (or whatever name you choose) for the parameter.

Homework Submission

Upload the Lisp file to Canvas, Assignments, Homework 9.