tag:blogger.com,1999:blog-521133349049053228.post1631382934800012102..comments2023-10-25T10:37:30.520+01:00Comments on mattyw: Another clojure macro tutorial (that no one should follow)Anonymoushttp://www.blogger.com/profile/11476264728618297782noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-521133349049053228.post-8236265101691454682015-05-22T19:47:46.216+01:002015-05-22T19:47:46.216+01:00I know this is an old post, but just have to leave...I know this is an old post, but just have to leave a comment.<br /><br />"The first rule of macro club is "Don't write macros"" It seems a lot of people (not the author of this article in particular but a lot of other people) don't have a clue what macros are, what they are used for, what their function is and hence come up with silly rules such as never use macros or always use functions before using macros.<br /><br />So heres my two cents:<br /><br />A function is used to perform a certain procedure (yes I know in functional languages we don't call them procedures any more but just bear with me) at run time, i.e. to execute a certain piece of code at run time with set of parameters provided at run time. A function performs the programs functionality i.e. adds numbers, displays windows on the screen, launches nuclear warheads etc.<br /><br />A macro is executed at Compile time, not run time, that is when the program is compiled and any forms containing the macro are encountered, the macro function is called and the forms are literally replaced with the result returned by the macro function. That is a macro is used to perform a code transformation not program functionality, a macro is used to manipulate code not actually perform any function of the code.<br /><br />So that leaves us with the following rule: if you need to perform program functionality, i.e. execute a piece of code at run time to produce a result from a set of inputs, use a function. If you need to transform a piece of code at compile time such as to make it understood by the core language, use a macro. That in my opinion is a much better rule than "never use macros", I think the latter is said by people coming from C/C++ #define macros which indeed should never be used, however Lisp macros are a whole different breed and there is no reason why they shouldn't be used, (otherwise you might as well program in Python or Ruby).Alexander Gutevhttps://www.blogger.com/profile/02605622990086203125noreply@blogger.comtag:blogger.com,1999:blog-521133349049053228.post-8135169833474011372012-08-16T00:35:40.207+01:002012-08-16T00:35:40.207+01:00This is a great way to look at how one might use m...This is a great way to look at how one might use macros. I haven't found a reason to yet.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-521133349049053228.post-57727041562627480292012-08-15T00:05:16.292+01:002012-08-15T00:05:16.292+01:00More importantly, for variable-arity functions, th...More importantly, for variable-arity functions, the only place you can reasonably put the docstring is before any of the arglists.Alan Malloyhttps://www.blogger.com/profile/11347579423754083087noreply@blogger.comtag:blogger.com,1999:blog-521133349049053228.post-17171574054724726812012-08-14T18:53:48.990+01:002012-08-14T18:53:48.990+01:00It's generally idiomatic Clojure to put the ar...It's generally idiomatic Clojure to put the arguments vector on its own line instead of on the same line as the function name as Python does, so that with a doc string you have<br /><br />(defn something <br /> "Adds to things together"<br /> [x y]<br /> (+ x y))<br /><br />and without it you have<br /><br />(defn something <br /> [x y]<br /> (+ x y))<br />Zephhttps://www.blogger.com/profile/10977471797021193972noreply@blogger.com