Assignment 6: Recursive Procedures and Strings
Due: Tuesday, March 14 by 10:30pm
Summary: In this assignment, you will use recursion techniques to solve several problems regarding strings and drawings.
Purposes: To practice using strings and tail recursion to solve various problems.
Collaboration: You must work with your assigned partners on this assignment. You must collaborate on every problem - do not break up the work so that one person works on problem 1, another on problem 2, and another on problem 3. (The “don’t break up the work” policy applies to every assignment. This note is just a reminder.) You may discuss this assignment with anyone, provided you credit such discussions when you submit the assignment.
Submitting: Send your answer to csc151-01-grader@grinnell.edu. The title of your email should have the form [CSC 151.01] Assignment 6 and should contain your answers to all parts of the assignment. I prefer that you put your answers in the body of the message, rather than as an attachment.
So that this assignment is a learning experience for everyone, we may spend class time publicly critiquing your work.
Assignment
Problem 1: Every kth element
Topics: lists, list recursion
a. Document and write a procedure, (remove-every-kth lst k), that
create a new list that contains all of the elements of lst except
those at indices 0, k, 2k, 3k, and so on. For example,
> (define example (string->list "an example"))
> example
'(#\a #\n #\space #\e #\x #\a #\m #\p #\l #\e)
> (remove-every-kth example 3)
'(#\n #\space #\x #\a #\p #\l)
> (remove-every-kth example 2)
'(#\n #\e #\a #\p #\e)
> (cons (car example) (remove-every-kth (cdr example) 2))
'(#\a #\space #\x #\m #\l)
> (remove-every-kth example 5)
'(#\n #\space #\e #\x #\m #\p #\l #\e)
b. Document and write a procedure, (select-every-kth lst k), that
creates a new list that contains only the elements of lst at indices
those at indices 0, k, 2k, 3k, and so on. For example,
> (define example (string->list "an example"))
> example
'(#\a #\n #\space #\e #\x #\a #\m #\p #\l #\e)
> (select-every-kth example 3)
'(#\a #\e #\m #\e)
> (select-every-kth example 2)
'(#\a #\space #\x #\m #\l)
Problem 2: Palindromes
Topics: tail recursion, strings and characters
A string is called a palindrome if reversing the characters of the string results in the same string. A few examples of palindromes are "radar", "civic", and "aabaa". Write and document a tail recursive procedure (palindrome? str) that checks to see if the string str is a palindrome. You may NOT use any string compare procedures for this problem (e.g. string=?, string<=?, etc.).
> (palindrome? "hello")
#f
> (palindrome? "civic")
#t
Problem 3: Drawing Self-Similar Fractals
Topics: recursion, GIMP tools
A self-similar fractal can be constructed by iteratively applying the same pattern to an image. One famous example of a self-similar fractal is the Sierpinski carpet which is generated by the pattern below.

The Sierpinski carpet can be produced recursively using the following process.
- Start with a square image (or portion of the image)
- Remove the center of the image by using an appropriate
call to
image-select-select-rectangle!and then filling with white. - Repeat steps 2 and 3 with the eight surrounding squares (top-left, top-middle, top-right, left-middle, right-middle, bottom-left, bottom-middle, and bottom-right).
- Stop when the portion of the image is “small enough”.
Below are the first four iterations of the Sierpinski carpet.

Write a procedure (sierpinski-carpet-region! image left top width height small-enough) that builds a Sierpinksi carpet by using the algorithm above. Your procedure should stop when the area of the region given by left, top, width, and height is less than small-enough.
> (context-set-bgcolor! "black")
'()
> (define c0 (image-new 100 100))
> (sierpinski-carpet-region! c0 0 0 100 100 200000)
> (define c1 (image-new 100 100))
> (sierpinski-carpet-region! c1 0 0 100 100 1000)
> (image-show c1)
> (define c2 (image-new 100 100))
> (sierpinski-carpet-region! c2 0 0 100 100 100)
> (define c3 (image-new 100 100))
> (sierpinski-carpet-region! c3 0 0 100 100 10)
c0
c1
c2
c3
If you are feeling particularly energetic, you might make your procedure work with non-square regions. Here’s what you might then get when you apply that procedure to everyone’s favorite image.
> (define kitten (image-load "/home/rebelsky/Desktop/kitten.jpg"))
> (sierpinski-carpet-region! kitten 0 0 (image-width kitten) (image-height kitten) 100)

Additional explorations
Instead of filling with the regions with white, one of the faculty decided to paste the image instead. Here are a few examples of what they ended up with.

Another faculty member decided to fill with a random color, rather than with white. Here are a few examples of what they produced.

We think Damien Hirst could make bundles on these.
If you have the inclination, you may try to create other variants of the Sierpinski carpet.
Acknowledgments
The pattern image and the first four iterations image from this problem are from Paul Bourke’s web page on self-similar fractals.
This assignment is based on one written by Charlie Curtsinger and Titus Klinge (or at least we think it is).