When people complain about the syntax and/or lisp in general I think they are are confused by the concepts and not the language itsself. For example I hardly needed a day for recursion, conditionals and the parenthesis structure, but I needed half a week just to get the applicative operators and to learn why programming with them is a good idea. I think many people are confused by the complex concepts like functional programming, recursion, functions as primary objects, the extended list navigation functions (like first, cdar, rest) and the not always intuitive list building functions (list, append, cons). After I "got" those they are easy to use and effective.
Here is my problem though: Apparently macros are one of the few thing which makes Lisp different and better than other modern high-level languages like Python or Ruby (I am not talking about low-level languages like C). And Macros are "responsible" for the typical lisp code with numerous parens and the compact not line oriented structure. So, all in all, macros seem important.
But I can't figure out why they are a powerful tool. The behave like functions: If lisp finds an object after the opening parens it is assumed to be a function/macro. The symbol-cell and the corresponding call is triggered, the arguments evaluated (functions) or taken (macro) and some list is returned. Why are macros such a good idea?
My teaching book defines two differences between macros and functions:
1) Macros dont eval their arguments
2) Macros return List-Code which is immeaditly evaluated
I dont see any possibilities for the first point to have a effect: Suppose you have some babble input and want them to communicate with your system. So you build a parser which morphs a string input into useful code. Considering the high number of sequence operations I think macros are not really needed. You just take string or list as input, build tokens and modify them. The point here is: You can evaluate your input everytime, because either your input makes sense from a lisp point-of-view (and you want them to be evaluated) or you take a string/list as a selfevaluating object and return them after you parsed them (which is needed anyway).
And the second point makes no sense too: A function returns data. If a macro returns code which is evaluated it must, at the end, produce data.
Last point: Macros can define functions or variables at the top level. I dont think that is not that useful either because functions can return functions and set variables. While it might be nice to write a function defining macro a function which returns a function does a similar thing.
Code: Select all
;not tested
(defmacro adder1 (name arg)
`(defun ,name (input)
(+ input ,arg))
;function does just as well
(defun adder (arg)
(lambda (d) (+ d arg)))
a) examples where macros are desperatly needed
b) some patterns where macros are used
c) explain how to build an embedded language on top of lisp like I heard so many times