Last week, our Chicago office hosted a meetup that explored functional programming using Swift hosted by one of our instructors, Fiaz Sami.  In the presentation, we took a step back from defining functional programming in technical terms, and instead defined functional programming as a thought process. We illustrated this concept using M.C. Escher’s Square Limit, which is almost a perfect visual representation of the concept of functional programming.

A nice feature of Square Limit is that as your eye moves from the center to the edge, you can see an unfolding process of interlocking shapes that get smaller and smaller as the eye approaches the edge of the drawing.  This concept was simplified for our purposes. In Swift, we made a single function that allowed us to perform a simple operation: drawing a single view to the right of an existing view.  We used this function as a building block to approximate a simplified version of Square Limit programatically.

Because of the sequential nature of Square Limit, we introduced recursion as a way to create such an effect.  We demonstrated the concept of recursion by illustrating how to use it to add two numbers.  We then applied recursion to the basic function to copy a square to the right of an input square.

This process of copying to the right was then expanded by creating a new function that copied a square to the bottom of an input square.  This gave us a new building block that was used to create one quarter of the Square Limit approximation.

At this point, we took a step back and examined the code that we were using to build our Square Limit reproduction from a higher level.  We pointed out that there were only two functions that we were using as basic building blocks (“copyRight” and copyBottom”).  We implemented “copyAbove” and “copyLeft” functions.  This allowed us to complete the entire reproduction of the Square Limit drawing using only code and functional programming in Swift.