Comp210 Lecture # 26    Spring 2003

More Divde-and-Conquer Generative Recursion:

Sierpinski Gaskets

Sierpinski gaskets are a type of fractal. They have the property of being "self-similar", that is, they are made of sub-parts that are similar (in the Euclidean geometry sort of way) to the whole. The Sierpinski gasket is a triangle formed of triangles formed of triangles, etc. etc..

In this and the following lecture, we will explore how we would go about writing code to display a Sierpinski gasket and how we can use the tools of abstraction we've already developed to create a more general fractal generator.

Most of the discussion will take place live in class, so there will be a minimum of notes on the web. Be sure to take good personal notes during class!

The most important parts of this whole process are the questions that are asked at each stage. Be sure to record what questions were raised as this may be more important than the answers. The answers pertain only to today's problem, while the questions apply for every problem.

 

What's are we working with?

We need to understand what we are working with before we can possibly write code for it. What is a Sierpinski gasket anyway?

A Sierpinski gasket is either

-- a triangle made of straight lines, or

-- a triangle made of Sierpinski gaskets.

If that don't reek of recursion, I don't know what does!

We can clearly see that a Sierpinski gasket is expressible in terms of a base case and an inductive case.

What we'd like to do is to write a functions that will enable us to

  1. Create a n-level Sierpinski gasket
  2. Draw an n-level Sierpinski gasket

Choices: Create and draw separately or create and draw all at once. We'll first try them separately, then we'll look at doing it all at once.

 

Hold on a second....

Does this all really about just Sierpinski gaskets? Does it really matter that we are dealing with triangles and triangles-of-triangles? Is that what we should be programming for?

 

We will write some simple code first, to get a feel for what is going on:

  1. Define a Sierpinski gasket
  2. Make a base case Sierpinski gasket of a particular size -- need a "factory" to make things for us.
  3. Draw a base case Sierpinski gasket
  4. Make an inductive case Sierpinski gasket from a base case gasket -- need another factory.
  5. Draw an inductive case gasket.
  6. Combine both base case and inductive case factories into a single factory.
  7. Construct visitors
  8. Construct factories for visitors
  9. etc. etc.

 

Factories

Factories are functions that make other structures or functions for the purposes of abstracting or encapsulating the construction process.

  • Factories encapsulate and abstract the construction (instantiations) of structures and functions.
    • This enables one to construct something without knowing how to construct it.
    • This enables one to construct something without knowing exactly what is being constructed.
    • This enables us to hide the complexities of the construction of large entities.
  • Through the use of closures, a factory can produce multiple outputs (.e.g. lambdas) that can remain connected and thus exhibit cohesive and correlated behavior.
    • This enables us to create a set of functions that are matched to each other, e.g. base and inductive case functions.
    • This enables us to deal with a set of related functions as a single unit.

     

Factories often involve curried functions.

Just returning a function or a structure from a function doesn't make it a factory. Being a factory also involves the semantics (the meaning) in which it is being used.

For more information, see this web page on the Abstract Factory Design Pattern

 

The day's code can be found here: lec26.scm

Pre-written code that might be useful: sierpinski.scm <-- this is not "the solution". The solution depends on the class discussion.

 

 

 

 

 

 

 

©2003 Stephen Wong