Scheme Programming Exercises

Like the intro lesson, visit http://repl.it/languages/Scheme to do your work. As you finish each exercise, you should save a copy of your code somewhere.

Exercise 1: sumOneToN (10pts)

Using the Fibonacci function from the Scheme intro as a guide, write a function called sumOneToN that takes a single argument, n , and returns the sum from 1 to n . Calling the function should yield results like these:

   
(sumOneToN 1)
=> 1
(sumOneToN 2)
=> 3
(sumOneToN 5)
=> 15
   
  

To reduce the difficulty, consider writing a Java method that does the same thing first. Note that it should be recursive!

Exercise 2: invertedTriangle (10pts)

Earlier this year, you wrote a Java method to print triangles looking like this:

   
*****
****
***
**
*
   
  

Your job now is to write a recursive Scheme function called invertedTriangle that takes an argument, n , and prints a triangle with a maximum width of n stars (asterisks).

For example:

   
(invertedTriangle 4)
****
***
**
*
   
  

To help you, consider these statements:

   
; returns a string with 6 ^s:

(make-string 6 #\^)
=> "^^^^^^"

; note that the pound-backslash is there to escape the character to be repeated

; printing a returned string:

(print (make-string 40 #\=))
========================================
   
  

Like the last exercise, it might be easier if you first write a similar recursive method in Java.

Exercise 3: uprightTriangle (5pts)

By making a small modification to invertedTriangle above, you can turn the triangle upside down. Write uprightTriangle so that it works this way;

   
(uprightTriangle 5)
*
**
***
****
*****