vindarel / lisp-journey

Discovering the Common Lisp ecosystem. https://lisp-journey.gitlab.io/
2 stars 0 forks source link

blog/common-lisp-macros-by-example-tutorial/ #25

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

Common Lisp Macros By Example Tutorial - Lisp journey

I have recently edited and somewhat expanded the macros page on the Common Lisp Cookbook. I find it may more legible and reader friendly, so I reproduce it below (however, I cut two parts so than you get the essential).You’d better read it on the Cookbook: https://lispcookbook.github.io/cl-cookbook/macros.htmlThe word macro is used generally in computer science to mean a syntactic extension to a programming language. (Note: The name comes from the word “macro-instruction,” which was a useful feature of many second-generation assembly languages.

https://lisp-journey.gitlab.io/blog/common-lisp-macros-by-example-tutorial/

yzhouxc commented 2 years ago

Exercise: Try writing the new version of the macro without using backquote. If you can’t do it, you have done the exercise correctly, and learned what backquote is for!

I do not understand why. Did you mean "if you can do it"?

vindarel commented 2 years ago

The quoted text says "if you can NOT do it". Does that answer? It's a silly way to say that we need the macros mechanism.

(in fact I inherited these "exercises" from the initial text and I'm not a fan)

matskew commented 2 years ago

Doesn't this replicate the macro without using backquote?

(defmacro setq3 (v1 v2 e)
  (let ((tempvar (gensym)))
    (list 'let (list (list tempvar e))
          (list 'progn (list 'setq v1 tempvar)
                (list 'setq v2 tempvar)))))
vindarel commented 2 years ago

Indeed. So with backquote we can avoid the many "list", the macro code looks like more the final code. Also it isn't always possible to have an equivalent with "list" (like when we need ,@).

wongjiahau commented 1 year ago

Thanks!

vindarel commented 1 year ago

📢 NEW!

Everyone, for those of you who like videos, I published a big chapter with 17 videos on macros on my course! https://lisp-journey.gitlab.io/blog/17-new-videos-on-common-lisp-macros/