Code Monkey home page Code Monkey logo

Comments (3)

orangeduck avatar orangeduck commented on July 17, 2024

You're correct that you can perfectly well use macros instead of Q-Expressions and essentially you can get the same thing - the quote macro is a bit like the Q-Expression indeed. There are subtle differences and pros and cons on both sides. Probably a real macro system is better for a kind of industrial strength Lisp, but I find Q-Expressions kind of nice in terms of simplicity and ease of learning.

To be a bit more precise about the comparison - a Macro is like a function that does not evaluate its arguments first: so if def is a macro then (def x blah) is kind of like (def {x blah}) using Q-Expressions. Some macros are also special macros and do special things e.g. assigning variable names, while in our lisp some functions are special functions and do things like assigning variable names.

Some arguments in favor of Q-Expressions over Macros:

  • With Macros there is no syntax distinguishing between a function and a macro. The only way to know if (blah x y) is going to evaluate x or not is to know if blah is a macro rather than a function. For this reason I think Q-Expressions can be easier spot and to intuitively understand.
  • Because you can write functions that return Q-Expressions you can build a Lisp using fewer builtin functions (i.e. just assignment and lambda), making things more homoiconic and simple.

Some arguments against Q-Expressions over Macros:

  • Q-Expressions make lexical scope difficult to implement compared to Macros, which can cause obscure and difficult variable shadowing bugs due to dynamic scope. (However, a hygienic Macro system is not that easy to implement either).
  • In some places you need to work around things being eagerly evaluated by wrapping them in Q-Expressions e.g. the head function.
  • Q-Expressions are fairly unusual/uncommon in other Lisps.

I actually think either is fine - if you want to switch the language to using Macros I think that is a pretty good exercise 👍

from buildyourownlisp.

lewismj avatar lewismj commented on July 17, 2024

Thanks, I managed a first cut of a C++ version https://github.com/lewismj/inky , I'll update my docs, to better explain things re: Q-Expressions and Macros. Enjoyable experience. Picked this up as an exercise to learn Spirit - I was going to try to use Boost Spirit. I'm familiar with FastParse (Scala) and Parsec (Haskell) but ... gave up with Spirit.

from buildyourownlisp.

lewismj avatar lewismj commented on July 17, 2024

Thanks very much for the explanations. Really appreciated!

from buildyourownlisp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.