A circle of Racketeers

About a math circle session that happend at our institute.

A few weeks back, the math circle being run by our institute organized a session involving a bit of computer programming.

I and some of my friends were there to help the participants. It was fun and in this blog post, I try to describe what happened in the session as a means of recalling the memory of that day.

The person in charge of the session used the programming language named racket, a language originally meant to be used to teach computer programming which has evolved into much more than that. I heard that there is even a framework written in racket to make web applications.

The ultimate aim was to get the participants familiar with the concept of recursion without them realizing it. True to the spirit of math circle where the idea is to let school students discover new things in a fun way. They are not lectures or classes, just some fun activities.

We used the slideshow 'language' of racket. I include ASCII-art depictions of the actual image output generated.

Considering that many people who encounter a lisp-like language find it 'difficult' to use, I was surprised to see that the children got quite comfortable with racket in no time. Probably due to the thoughtful way in which it was presented to them.

Then again, those who find lisp difficult to get used to are often those who already got accustomed to languages like C, C++, python, etc. It's like windows users finding FOSS operating systems 'difficult' to use at first.

I, for one, wasn't familiar with racket though I have been able to put together an emacs configuration in elisp by copying snippets off the internet. But thanks to the guidance of our teachers, I am no longer a novice at functional programming style and the basics of racket syntax was easy enough.

Being a language designed for teaching programming, Racket comes with its own IDE called DrRacket.
(Though an emacs mode for racket is available, we used DrRacket for the session as we couldn't expect the participants to be familiar with emacs!)


The session began by the participants doing simple arithmetic like addition of two numbers using racket. This gave them a taste of what racket was like.

> (+ 1 2)
3

At first, many found the prefix notation strange. But then they got used to it.

Then instead of just two numbers, they were asked to find the sum of a set of numbers.

Instruction was given for the kids to find the sum using racket. It was presented as a number game. The first one who found the correct sum wins.

> (+ 16 35 9 23 53 64 22)
222

We never kept track of the winners and the children didn't seem to have complaints about it either. But the satisfaction of finding the sum using computer was visible on each of their faces.

Next, the participants were shown how to give names to values. We started off with a constant, the value of pi.

(define pi 3.14)

then used it to make to make non-constants. Like the area of a circle:

(define (area r)
  (* pi r r))

and then tried it out:

> pi
3.14

> (area 5)
78.5

In between, the children went on to define constants named after themselves, their favorite sports players, actors, etc.

After giving some time for them to experiment on their own, participants were shown how to draw shapes. Rectangles first and then circles. They figured out that squares can be made using the 'technique' to draw rectangles if both length and breadth were made the same.

Then filled rectangles, circles, ellipses, etc were shown. Particularly popular was the way to colour all these shapes that they had made.

Soon, a lot of creativity was on display on the computer monitors as a result of the children's experimentation.

We saw lot of interesting creations, including colourful depictions of trains, wrist watch, cats, human figures and desktop computers.

FIFA world cup was going on at the time. Some of the particpants got busy drawing the likeness of the flags of their favourite football teams.
Came across many Argentina flags. Also present were loyal supporters of Brazil proudly showing off their colours though their team exited the world cup only the night before.

There were participants asking for a way to insert space between shapes, many asked for a way to draw triangles. Someone got interested in the way to draw a shape on top of another (overlays) but I don't think he got time to do that during the session itself. I have a hunch that he tried it out at home afterwards.

I myself was interested in drawing curves, but didn't dig deep enough into the racket documentation to find a way.

The participants seemed to have been well versed with colour names used in the web. I came across many unheard of colour names that the kids were using. At first I thought such colours didn't exist for racket, but they did in most cases.

One interesting question that I heard one of the kids asking was how they would draw all these shapes if the built-in methods to draw rectangles and circles weren't there.

For most of them, this was the first programming experience, though there were some who were already familiar with Python. Some of the participants were not that comfortable with using a desktop computer. Maybe it's because children these days are more familiar with mobile phones than desktop (or laptop) computers with an actual keyboard.

As was mentioned before, the aim of the session was to let the participants get hold of an understanding ofthe concept of recursion, without really telling them that it's recursion. In such a way that they will come to know what it is before they know what it is called.

This was done with the help of examples.

Zebra crossing

The next task was to draw a zebra crossing pattern. With alternating stripes of white and black. Sort of like this:

██░░██░░██░░██░░██░░██░░
██░░██░░██░░██░░██░░██░░
██░░██░░██░░██░░██░░██░░
██░░██░░██░░██░░██░░██░░
██░░██░░██░░██░░██░░██░░

Participants were asked to build the pattern step by step.

Like this:

z1       z2           z3               z4

██░░     ██░░██░░     ██░░██░░██░░     ██░░██░░██░░██░░
██░░     ██░░██░░     ██░░██░░██░░     ██░░██░░██░░██░░
██░░     ██░░██░░     ██░░██░░██░░     ██░░██░░██░░██░░
██░░     ██░░██░░     ██░░██░░██░░     ██░░██░░██░░██░░
██░░     ██░░██░░     ██░░██░░██░░     ██░░██░░██░░██░░

Which could be done something like this:

#lang slideshow

; Building blocks. Too lazy to define over and over again. :-)
(define black-bar
  (filled-rectangle 20 100))
(define white-bar
  (colorize black-bar "white"))

; Seeing some patterns.
(define z1
  (hc-append white-bar black-bar))
(define z2
  (hc-append z1 z1))
(define z3
  (hc-append z1 z2))
(define z4
  (hc-append z1 z3))

Then they were encouraged to find patterns. We were looking for them to find this pattern:

z2 = z1 + z1
z3 = z1 + z2
z4 = z1 + z3

Once they figured that out, they were able to have a method to generate arbitrarily long zebra crossing patterns.

(define (zebra n)
  (if (<= n 1) z1

; (zebra 4)

Chess board

We had run out of time by this point, but the person leading session was prepared with more things and had given us a glimpse of some of it earlier. One of them was the chess board pattern (or checkerboard, if you like).

Aim is to make a pattern like:

████░░░░████░░░░████░░░░████░░░░
████░░░░████░░░░████░░░░████░░░░
░░░░████░░░░████░░░░████░░░░████
░░░░████░░░░████░░░░████░░░░████
████░░░░████░░░░████░░░░████░░░░
████░░░░████░░░░████░░░░████░░░░
░░░░████░░░░████░░░░████░░░░████
░░░░████░░░░████░░░░████░░░░████
████░░░░████░░░░████░░░░████░░░░
████░░░░████░░░░████░░░░████░░░░
░░░░████░░░░████░░░░████░░░░████
░░░░████░░░░████░░░░████░░░░████
████░░░░████░░░░████░░░░████░░░░
████░░░░████░░░░████░░░░████░░░░
░░░░████░░░░████░░░░████░░░░████
░░░░████░░░░████░░░░████░░░░████

As in the case of the zebra crossing pattern, one could start by having a few patterns.

We can consider a board consisting of four cells as the board of size 1. Then boards of sizes 1, 2 and 3 would be like:

b1          b2                  b3

████░░░░    ████░░░░████░░░░    ████░░░░████░░░░████░░░░████░░░░
████░░░░    ████░░░░████░░░░    ████░░░░████░░░░████░░░░████░░░░
░░░░████    ░░░░████░░░░████    ░░░░████░░░░████░░░░████░░░░████
░░░░████    ░░░░████░░░░████    ░░░░████░░░░████░░░░████░░░░████
            ████░░░░████░░░░    ████░░░░████░░░░████░░░░████░░░░
            ████░░░░████░░░░    ████░░░░████░░░░████░░░░████░░░░
            ░░░░████░░░░████    ░░░░████░░░░████░░░░████░░░░████
            ░░░░████░░░░████    ░░░░████░░░░████░░░░████░░░░████
                                ████░░░░████░░░░████░░░░████░░░░
                                ████░░░░████░░░░████░░░░████░░░░
                                ░░░░████░░░░████░░░░████░░░░████
                                ░░░░████░░░░████░░░░████░░░░████
                                ████░░░░████░░░░████░░░░████░░░░
                                ████░░░░████░░░░████░░░░████░░░░
                                ░░░░████░░░░████░░░░████░░░░████
                                ░░░░████░░░░████░░░░████░░░░████

which can be made using something like:

; Building blocks
(define black-cell
  (filled-rectangle 20 20))
(define white-cell
  (colorize black-cell "white"))

; Experimenting
(define b1
  (vc-append
    (hc-append white-cell black-cell)
    (hc-append black-cell white-cell)))

(define b2
  (vc-append
    (hc-append b1 b1)
    (hc-append b1 b1)))

(define b3
  (vc-append
    (hc-append b2 b2)
    (hc-append b2 b2)))

By this point, it becomes easy to notice the pattern.

So we could have a function to make a board of size n.

(define (board n)
  (if (<= n 1) b1
    (vc-append
      (hc-append (board (- n 1)) (board (- n 1)))
      (hc-append (board (- n 1)) (board (- n 1))))))

Sierpiński carpet

Next illustration that the session leader had in mind was the Sierpiński carpet. Participants weren't given time to do this as it would've taken too much time. They were simply shown this and the way to think about making these shapes.

Roughly speaking, Sierpiński carpet consists of the pattern formed when a square shape is recursively removed from the middle of square such that after each removal the remaining portion consists of 8 squares of the same dimension as the removed square.

Looks like this:

Level 1               Level 2               Level 3

██████████████████    ██████████████████    ██████████████████
██████████████████    ██████████████████    ██  ████  ████  ██
██████████████████    ██████████████████    ██████████████████
██████████████████    ██████      ██████    ██████      ██████
██████████████████    ██████      ██████    ██  ██      ██  ██
██████████████████    ██████      ██████    ██████      ██████
██████████████████    ██████████████████    ██████████████████
██████████████████    ██████████████████    ██  ████  ████  ██
██████████████████    ██████████████████    ██████████████████

I tried to make a function to make this shape in racket. It sort of works, but it's not exactly nice so I'm not including it as part of this blog post.

A nice version of Sierpiński carpet in racket can be found here.

Anyway, if the pattern of level l is made by a function f, we could think of the carpet of level l (ie, f(l)) being made like:

f(l-1)    f(l-1)      f(l-1)
f(l-1)    blank(l)    f(l-1)
f(l-1)    f(l-1)      f(l-1)

where blank(l) denotes an empty space.

Just for the fun of it, here's a Sierpiński carpet of level 4 that I made:

██████████████████████████████████████████████████████
██  ████  ████  ████  ████  ████  ████  ████  ████  ██
██████████████████████████████████████████████████████
██████      ████████████      ████████████      ██████
██  ██      ██  ████  ██      ██  ████  ██      ██  ██
██████      ████████████      ████████████      ██████
██████████████████████████████████████████████████████
██  ████  ████  ████  ████  ████  ████  ████  ████  ██
██████████████████████████████████████████████████████
██████████████████                  ██████████████████
██  ████  ████  ██                  ██  ████  ████  ██
██████████████████                  ██████████████████
██████      ██████                  ██████      ██████
██  ██      ██  ██                  ██  ██      ██  ██
██████      ██████                  ██████      ██████
██████████████████                  ██████████████████
██  ████  ████  ██                  ██  ████  ████  ██
██████████████████                  ██████████████████
██████████████████████████████████████████████████████
██  ████  ████  ████  ████  ████  ████  ████  ████  ██
██████████████████████████████████████████████████████
██████      ████████████      ████████████      ██████
██  ██      ██  ████  ██      ██  ████  ██      ██  ██
██████      ████████████      ████████████      ██████
██████████████████████████████████████████████████████
██  ████  ████  ████  ████  ████  ████  ████  ████  ██
██████████████████████████████████████████████████████

Fun fact that I heard during the session: Sierpiński carpet design is used in antennas to be put inside small devices like phones. Because the 'holes' of different dimensions can handle signals of different frequencies.

Take away

At the end of the day, the children were excited to see all the illustrations that they ended up making. In effect, they learned the concept of recursion as a fun activity. In fact, I didn't hear the word recursion ever being mentioned.

When they get around to learning about recursion at school, these kids would be in for a pleasant surprise.

Acknowledgements: Thanks to Piyush sir, who led the session. He has got the source code for some of the content used in his presentation over here.