
Revised Edition
In addition to the original specifications, add the following functionality to your program. These features emphasize specific design issues and are meant to help you think about what it means to "hard-code" things within your program and how to provide a interface for programmer's to access flexibility when using your program.
Specifications
Your program should recognize the following additional syntax for expressions:
expression | syntax | semantics | examples |
---|---|---|---|
Assignment |
|
assigns an expression to a variable, if a variable name is used that has not previously been given a value, return a default value (like BLACK) note, variables should act as references to their expressions rather than copying/evaluating them on assignment (this allows variables to correctly evaluate their contents dynamically, but it has the side-effect that changes to variables are reflected in other expressions) |
a = [ 1, 0, 1 ]
bugs = a + x |
Multi-Argument Functions | a function that takes one or more expressions as its arguments
sum // returns sum of all the given expressions together min // returns minimum of the given expressions based on their intensity (i.e., grey scale value) max // returns maximum of the given expressions based on their intensity (i.e., grey scale value) average // returns average of all the given expressions |
sum(a, a + b, c * d / x)
min(min(sum(a, b), x+x), y) |
|
Postfix Unary Operator | appears after an expression
~ // convert a color to greyscale |
a~ (t + a * x)~ |
A user can explicitly save an expression to a named variable. Such variables serve the program's memory and can be used to refer to the saved expression later in other expressions.
To make it easier to generate expressions automatically rather than typing them in manually, do at least one of the following options:
- easily combine saved expressions into new expressions
- save each expression entered as a variable,
ans
, which represents the current expression and can be combined with the next expression entered - allow users to refer to the history of expressions in this session by entering their history number prefixed by "$" (i.e., "$3")
- save each expression entered as a variable,
- generate expressions randomly
- generate expressions based on a string
- "breed" new expressions by combining old expressions
To make it possible to generate other kinds of images (rather than simply evaluating them for each pixel), do at least one of the following options:
- iterate the expression to create fractals
- solve for the roots of the expression
- animate an expression over time rather than simply creating a single image
- a time parameter that varies from zero to one (or any range) turns any expression into an animation (if it goes zero to one and back to zero, then it can be looped)
- if breeding is implemented as well, the animation can be the "genetic cross-dissolves" between the parent and the child
For extra credit, make the "back-end" code more general and efficient by doing at least one of the following options:
- allow users to enter expressions without spaces between key tokens
- prune the expression trees based on useless arithmetic branches (e.g., multiplication by 1 or 0)
- recognize identical sub-expressions to avoid recalculating them when necessary
- recognize sub-expressions that do not depend on either x or y so they can be computed only once per image