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.
So: Implement 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 filter and 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 for loop?
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-loop.
C's 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 for loop:
for loop), and gives it an initial value.
for loops can have many, separated by commas.)false value causes the loop to exit).for loops can have many increments, separated by commas.)do), typically making use of the loop index.
for loop should be the value of the last "statement" in the loop body--again, like Clojures' do.Use all of: defmacro, loop, template, quote and unquote.
Turn in
for-loop macro, 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.