Math/CSE 1560 Lab 9

Exercise: Recursive procedures in Maple

Objective
The objective of this exercise is to test your familiarity with solving problems using recursion in Maple.

Grading
This lab exercise is meant to be completed in the lab, individually (Collaboration is NOT allowed for this exercise).

NOTE: This is a 2 hour lab. The submission server will close shortly after 4:30 pm. You will not be able to submit your work after that time.

No printouts will be accepted.

Starting off:

  1. Open Maple 12 by clicking on the icon marked as such. If Maple does not start, let the TA know immediately. If asked, choose to start a new worksheet.
  2. On the first 2 lines, enter your name and student number as comments -- start the lines with character #. Then save the file as `lab9.mw` by clicking on `File` and then `save as`.

Problems
Note: Some of these problems can be solved using loops or seq() or map(). To get credit for this assignment you must use recursion instead.

  1. (5 points) Write a procedure using recursion that takes as input a positive integer n and a positive integer num and outputs num lines each containg the number n. You are not allowed to use loops, or any function not covered in class.
  2. (5 points) Write a procedure using recursion that takes as input a positive integer and prints out a single 1, 2 2's, 3 3's,..., n n's. So when the input is 4 the output should be 1 2 2 3 3 3 4 4 4 4. It is fine if each number is on a separate line. You are not allowed to use loops, or any function not covered in class. You can use the procedure in the previous question in this question if you like.
  3. (5 points) Write a procedure using recursion that takes as input a list L and returns the sum of all the elements of the list that are not primes. You are not allowed to use loops.
    E.g. If L = [1,1,2,3,4,6] it should return 1+1+4+6 = 12.
  4. (5 points) Write a procedure using recursion that takes as input a list L of positive integers. It should print an error message if the list is of odd length; otherwise it returns the sum L[1]*L[2]+L[3]*L[4]+....+L[nops(L)-1]*L[nops(L)]. You are not allowed to use loops.

Final steps

  1. Save your worksheet.
  2. Submit the assignment as lab 9 in Moodle (the URL is moodle.math.yorku.ca). You can upload several times, but remember to submit using the "send for marking" at the end ONCE - otherwise it may not be sent.
  3. You are done with this assignment. Remember to logout before you leave the lab.