<p>I have read a lot that LISP can redefine syntax on the fly, presumably with macros.  I am curious how far does this actually go?  Can you redefine the language structure so much that it borderline becomes a compiler for another language?  For example, could you change the functional nature of LISP into a more object oriented syntax and semantics, maybe say having syntax closer to something like Ruby?</p>

<p>Especially, is it possible to get rid of the parenthesis hell using macros?  I have learned enough (Emacs-)LISP to customize Emacs with my own micro-features, but I am very curious how far macros can go in customizing the language.</p>

[{'answer_id': 1992, 'author': 'sparkes', 'author_id': 269, 'author_profile': 'https://Stackoverflow.com/users/269', 'pm_score': 2, 'selected': False, 'text': "<p>If you want lisp to look like Ruby use Ruby. </p>\n\n<p>It's possible to use Ruby (and Python) in a very lisp like way which is one of the main reasons they have gained acceptance so quickly.</p>\n"}, {'answer_id': 1993, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 1, 'selected': False, 'text': "<p>@sparkes</p>\n\n<p>Sometimes LISP is the clear language choice, namely Emacs extensions.  I'm sure I could use Ruby to extend Emacs if I wanted to, but Emacs was designed to be extended with LISP, so it seems to make sense to use it in that situation.</p>\n"}, {'answer_id': 3313, 'author': 'John with waffle', 'author_id': 279, 'author_profile': 'https://Stackoverflow.com/users/279', 'pm_score': 1, 'selected': False, 'text': "<p>It's a tricky question.  Since lisp is already structurally so close to a parse tree the difference between a large number of macros and implementing your own mini-language in a parser generator isn't very clear.  But, except for the opening and closing paren, you could very easily end up with something that looks nothing like lisp.</p>\n"}, {'answer_id': 27638, 'author': 'Luís Oliveira', 'author_id': 2967, 'author_profile': 'https://Stackoverflow.com/users/2967', 'pm_score': 4, 'selected': False, 'text': '<p>Regular macros operate on lists of objects. Most commonly, these objects are other lists (thus forming trees) and symbols, but they can be other objects such as strings, hashtables, user-defined objects, etc. These structures are called <a href="http://en.wikipedia.org/wiki/S-expression" rel="noreferrer">s-exps</a>.</p>\n\n<p>So, when you load a source file, your Lisp compiler will parse the text and produce s-exps. Macros operate on these. This works great and it\'s a marvellous way to extend the language within the spirit of s-exps.</p>\n\n<p>Additionally, the aforementioned parsing process can be extended through "reader macros" that let you customize the way your compiler turns text into s-exps. I suggest, however, that you embrace Lisp\'s syntax instead of bending it into something else.</p>\n\n<p>You sound a bit confused when you mention Lisp\'s "functional nature" and Ruby\'s "object-oriented syntax". I\'m not sure what "object-oriented syntax" is supposed to be, but Lisp is a multi-paradigm language and it supports object-oriented programming <em>extremelly</em> well.</p>\n\n<p>BTW, when I say Lisp, I mean <a href="http://en.wikipedia.org/wiki/Common_Lisp" rel="noreferrer">Common Lisp</a>.</p>\n\n<p>I suggest you put your prejudices away and <a href="http://gigamonkeys.com/book/" rel="noreferrer">give Lisp an honest go</a>.</p>\n'}, {'answer_id': 27653, 'author': 'Sergio Acosta', 'author_id': 2954, 'author_profile': 'https://Stackoverflow.com/users/2954', 'pm_score': 4, 'selected': False, 'text': "<p>I'm not a Lisp expert, heck I'm not even a Lisp programmer, but after a bit of experimenting with the language I came to the conclusion that after a while the parenthesis start becoming 'invisible' and you start seeing the code as you want it to be. You start paying more attention to the syntactical constructs you create via s-exprs and macros, and less to the lexical form of the text of lists and parenthesis.</p>\n\n<p>This is specially true if you take advantage of a good editor that helps with the indentation and syntax coloring (try setting the parenthesis to a color very similar to the background).</p>\n\n<p>You might not be able to replace the language completely and get 'Ruby' syntax, but you don't need it. Thanks to the language flexibility you could end having a dialect that feels like you are following the 'Ruby style of programming' if you want, whatever that would mean to you.</p>\n\n<p>I know this is just an empirical observation, but I think I had one of those Lisp enlightenment moments when I realized this.</p>\n"}, {'answer_id': 62267, 'author': 'HD.', 'author_id': 6525, 'author_profile': 'https://Stackoverflow.com/users/6525', 'pm_score': 3, 'selected': False, 'text': '<p>Yes, you can fundamentally change the syntax, and even escape "the parentheses hell".  For that you will need to define a new reader syntax.  Look into reader macros.</p>\n\n<p>I do suspect however that to reach the level of Lisp expertise to program such macros you will need to immerse yourself in the language to such an extent that you will no longer consider parenthese "hell".  I.e. by the time you know how to avoid them, you will have come to accept them as a good thing.</p>\n'}, {'answer_id': 63737, 'author': 'Eric Normand', 'author_id': 7492, 'author_profile': 'https://Stackoverflow.com/users/7492', 'pm_score': 6, 'selected': True, 'text': '<p>That\'s a really good question.</p>\n\n<p>I think it\'s nuanced but definitely answerable:</p>\n\n<p>Macros are not stuck in s-expressions.  See the LOOP macro for a very complex language written using keywords (symbols).  So, while you may start and end the loop with parentheses, inside it has its own syntax.</p>\n\n<p>Example:</p>\n\n<pre><code>(loop for x from 0 below 100\n      when (even x)\n      collect x)\n</code></pre>\n\n<p>That being said, most simple macros just use s-expressions.  And you\'d be "stuck" using them.</p>\n\n<p>But s-expressions, like Sergio has answered, start to feel right.  The syntax gets out of the way and you start coding in the syntax tree.</p>\n\n<p>As for reader macros, yes, you could conceivably write something like this:</p>\n\n<pre><code>#R{\n      ruby.code.goes.here\n  }\n</code></pre>\n\n<p>But you\'d need to write your own Ruby syntax parser.</p>\n\n<p>You can also mimic some of the Ruby constructs, like blocks, with macros that compile to the existing Lisp constructs.</p>\n\n<pre><code>#B(some lisp (code goes here))\n</code></pre>\n\n<p>would translate to</p>\n\n<pre><code>(lambda () (some lisp (code goes here)))\n</code></pre>\n\n<p>See <a href="http://rottcodd.wordpress.com/2007/11/29/lambda-shortcut/" rel="noreferrer">this page</a> for how to do it.</p>\n'}, {'answer_id': 74199, 'author': 'Jerry Cornelius', 'author_id': 6594, 'author_profile': 'https://Stackoverflow.com/users/6594', 'pm_score': 4, 'selected': False, 'text': "<p>Parenthesis hell? I see no more parenthesis in:</p>\n\n<pre><code>(function toto)\n</code></pre>\n\n<p>than in:</p>\n\n<pre><code>function(toto);\n</code></pre>\n\n<p>And in</p>\n\n<pre><code>(if tata (toto)\n  (titi)\n  (tutu))\n</code></pre>\n\n<p>no more than in:</p>\n\n<pre><code>if (tata)\n  toto();\nelse\n{\n  titi();\n  tutu();\n}\n</code></pre>\n\n<p>I see less brackets and ';' though.</p>\n"}, {'answer_id': 76012, 'author': 'Sean Harrison', 'author_id': 13410, 'author_profile': 'https://Stackoverflow.com/users/13410', 'pm_score': 5, 'selected': False, 'text': '<p>Yes, you can redefine the syntax so that Lisp becomes a compiler. You do this using "Reader Macros," which are different from the normal "Compiler Macros" that you\'re probably thinking of.</p>\n\n<p>Common Lisp has the built-in facility to define new syntax for the reader and reader macros to process that syntax. This processing is done at read-time (which comes before compile or eval time). To learn more about defining reader macros in Common Lisp, see the Common Lisp Hyperspec -- you\'ll want to read <a href="http://www.lispworks.com/documentation/HyperSpec/Body/02_.htm" rel="noreferrer">Ch. 2, "Syntax"</a> and <a href="http://www.lispworks.com/documentation/HyperSpec/Body/23_.htm" rel="noreferrer">Ch. 23, "Reader"</a>. (I believe Scheme has the same facility, but I\'m not as familiar with it -- see the <a href="http://arclanguage.org/install" rel="noreferrer">Scheme sources</a> for the <a href="http://arclanguage.org" rel="noreferrer">Arc programming language</a>).</p>\n\n<p>As a simple example, let\'s suppose you want Lisp to use curly braces rather than parentheses. This requires something like the following reader definitions:</p>\n\n<pre><code>;; { and } become list delimiters, along with ( and ).\n(set-syntax-from-char #\\{ #\\( )\n(defun lcurly-brace-reader (stream inchar) ; this was way too easy to do.\n  (declare (ignore inchar))\n  (read-delimited-list #\\} stream t))\n(set-macro-character #\\{ #\'lcurly-brace-reader)\n\n(set-macro-character #\\} (get-macro-character #\\) ))\n(set-syntax-from-char #\\} #\\) )\n\n;; un-lisp -- make parens meaningless\n(set-syntax-from-char #\\) #\\] ) ; ( and ) become normal braces\n(set-syntax-from-char #\\( #\\[ )\n</code></pre>\n\n<p>You\'re telling Lisp that the { is like a ( and that the } is like a ). Then you create a function (<code>lcurly-brace-reader</code>) that the reader will call whenever it sees a {, and you use <code>set-macro-character</code> to assign that function to the {. Then you tell Lisp that ( and ) are like [ and ] (that is, not meaningful syntax).</p>\n\n<p>Other things you could do include, for example, <a href="http://www.weitz.de/cl-interpol/" rel="noreferrer">creating a new string syntax</a> or using [ and ] to enclose in-fix notation and process it into S-expressions. </p>\n\n<p>You can also go far beyond this, redefining the entire syntax with your own macro characters that will trigger actions in the reader, so the sky really is the limit. This is just one of the reasons why <a href="http://www.paulgraham.com/icad.html" rel="noreferrer">Paul Graham</a> and <a href="http://gigamonkeys.com/book/introduction-why-lisp.html" rel="noreferrer">others</a> keep saying that Lisp is a good language in which to write a compiler.</p>\n'}, {'answer_id': 76027, 'author': 'jfm3', 'author_id': 11138, 'author_profile': 'https://Stackoverflow.com/users/11138', 'pm_score': 4, 'selected': False, 'text': '<p>Over and over again, newcomers to Lisp want to "get rid of all the parenthesis."  It lasts for a few weeks.  No project to build a serious general purpose programming syntax on top of the usual S-expression parser ever gets anywhere, because programmers invariably wind up preferring what you currently perceive as "parenthesis hell."  It takes a little getting used to, but not much!  Once you do get used to it, and you can really appreciate the plasticity of the default syntax, going back to languages where there\'s only one way to express any particular programming construct is really grating.</p>\n\n<p>That being said, Lisp is an excellent substrate for building Domain Specific Languages.  Just as good as, if not better than, XML.</p>\n\n<p>Good luck!</p>\n'}, {'answer_id': 78672, 'author': 'Attila Lendvai', 'author_id': 14464, 'author_profile': 'https://Stackoverflow.com/users/14464', 'pm_score': 2, 'selected': False, 'text': '<p>see this example of how reader macros can extend the lisp reader with complex tasks like XML templating:</p>\n\n<p><a href="http://common-lisp.net/project/cl-quasi-quote/present-class.html" rel="nofollow noreferrer">http://common-lisp.net/project/cl-quasi-quote/present-class.html</a></p>\n\n<p>this <strong>user library</strong> compiles the static parts of the XML into UTF-8 encoded literal byte arrays at compile time that are ready to be write-sequence\'d into the network stream. and they are usable in normal lisp macros, they are orthogonal... the placement of the comma character influences which parts are constant and which should be evaluated at runtime.</p>\n\n<p>more details available at: <a href="http://common-lisp.net/project/cl-quasi-quote/" rel="nofollow noreferrer">http://common-lisp.net/project/cl-quasi-quote/</a></p>\n\n<p>another project that for Common Lisp syntax extensions: <a href="http://common-lisp.net/project/cl-syntax-sugar/" rel="nofollow noreferrer">http://common-lisp.net/project/cl-syntax-sugar/</a></p>\n'}, {'answer_id': 82449, 'author': 'user15700', 'author_id': 15700, 'author_profile': 'https://Stackoverflow.com/users/15700', 'pm_score': 3, 'selected': False, 'text': '<p>What you are asking is somewhat like asking how to become an expert chocolatier so that you can remove all that hellish brown stuff from your favourite chocolate cake.</p>\n'}, {'answer_id': 101022, 'author': 'Nowhere man', 'author_id': 400277, 'author_profile': 'https://Stackoverflow.com/users/400277', 'pm_score': 1, 'selected': False, 'text': '<p>One of the uses of macros that blew my mind was the compile-time verification of SQL requests against DB.</p>\n\n<p>Once you realize you have the full language at hand at compile-time, it opens up interesting new perspectives. Which also means you can shoot yourself in the foot in interesting new ways (like rendering compilation not reproducible, which can very easily turn into a debugging nightmare).</p>\n'}, {'answer_id': 172107, 'author': 'Dan Weinreb', 'author_id': 25275, 'author_profile': 'https://Stackoverflow.com/users/25275', 'pm_score': 4, 'selected': False, 'text': '<p>The best explanation of Lisp macros I have ever seen is at</p>\n\n<p><a href="https://www.youtube.com/watch?v=4NO83wZVT0A" rel="nofollow noreferrer">https://www.youtube.com/watch?v=4NO83wZVT0A</a></p>\n\n<p>starting at about 55 minutes in.  This is a video of a talk given by Peter Seibel, the author of "Practical Common Lisp", which is the best Lisp textbook there is.</p>\n\n<p>The motivation for Lisp macros is usually hard to explain, because they really come into their own in situations that are too lengthy to present in a simple tutorial.  Peter comes up with a great example; you can grasp it completely, and it makes good, proper use of Lisp macros.</p>\n\n<p>You asked: "could you change the functional nature of LISP into a more object oriented syntax and semantics".  The answer is yes.  In fact, Lisp originally didn\'t have any object-oriented programming at all, not surprising since Lisp has been around since way before object-oriented programming!  But when we first learned about OOP in 1978, we were able to add it to Lisp easily, using, among other things, macros.  Eventually the Common Lisp Object System (CLOS) was developed, a very powerful object-oriented programming system that fits elegantly into Lisp.  The whole thing can be loaded as an extension -- nothing is built-in!  It\'s all done with macros.</p>\n\n<p>Lisp has an entirely different feature, called "reader macros", that can be used to extend the surface syntax of the language.  Using reader macros, you can make sublanguages that have C-like or Ruby-like syntax.  They transform the text into Lisp, internally.  These are not used widely by most real Lisp programmers, mainly because it is hard to extend the interactive development environment to understand the new syntax.  For example, Emacs indentation commands would be confused by a new syntax.  If you\'re energetic, though, Emacs is extensible too, and you could teach it about your new lexical syntax.</p>\n'}]
['https://Stackoverflow.com/questions/1988', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/122/']