Macros let you "extend the language," and are widely considered (by Lisp enthusiasts) to be what makes Lisp so great. So this assignment is about extending Clojure with macros. The question I had to solve was, what extension or extensions to make?
The most useful extensions are "large" ones. Provide code to support expert systems, like Clips or Jess. Provide pattern matching and backtracking, like Prolog. Build a sane GUI API, like REBOL/View or Ruby Shoes. There isn't that much that's really useful for small programs.
while loops? Already been done, available on the Web. Besides, Clojure already has
while loops (really--check the documentation). Well, how about list comprehensions? That's useful, and just a combination of
map. Oh, already been done. Besides, Clojure already has list comprehensions.
So everything is either too big a project, or already done. What does that leave? Something totally useless, of course. And what could be more useless in Clojure than a C-style
Well, that's been done, too. Amazing. But at this point I'm out of ideas for the "right size" macro, so a
for loop it is.
Implement a C-style
for loop in Clojure, using macros. Call it
for loops are more complex than most people realize or ever use, so we'll just go with the basic functionality that everyone knows. A
forloop), and gives it an initial value.
forloops can have many, separated by commas.)
falsevalue causes the loop to exit).
forloops can have many increments, separated by commas.)
do), typically making use of the loop index.
forloop should be the value of the last "statement" in the loop body--again, like Clojures'
Use all of:
loop, template, quote and unquote.
for-loopmacro, and using it in two or three examples.
readme.txt, describing how to use your macro, and giving an example use and an example macro expansion.