Re: I dont get Macros
Posted: Mon Feb 01, 2010 7:05 am
Well, aif would be more like:Destruct1 wrote:I probably dont get that, but it is very easy:
So here is the point: Macros can be easily implemented by other thingsCode: Select all
def aif (long_calculation, further_func): if (long_calucaltion) != 0: return (further_func (long_calculation)) else: return (0) # ??
Code: Select all
(aif (some-function)
(do-something-with it)
(do-something-else))
Code: Select all
(let ((it (some-function)))
(if it
(do-something-with it)
(do-something-else)))
Your suggested workaround for aif (using functions) would not be good enough. The easiest way to avoid using aif is just writing its expansion, which is not ugly at all. But it is too long and repetitive, and it will take your time while you could be working on something else in your code. The whole point about aif is its simplicity. It makes your code more direct and will keep you away from the tiny details of you code you just don't care about - in this case, the declaration of a variable just to hold a value before you can test if it is true and use the result.
The macro aif alone can't do very much with the transparency of your code if it is the only macro you have, but, having a small cosmetic macro here, another one there, and another utility function over there, in the end your code will be much more transparent.
The good Lisp style is about brevity and transparency. You should not write only what you want to do, not more, not less. If you want to do something with an open file, you just use with-open-file, instead of telling the compiler what to do (which would be you saying "declare a variable, open a file and assign the result to that variable, check if the file was successfully open, do something, then close the file"). The interface to the functionality of your code should be clear instead of requiring the user to do small repetitive ugly things to work with it.
In the end, Lisp libraries look much more like a DSL created on top of Lisp than a library itself. Each one have its own syntax to make its use simpler and clearer.