Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparison of Common Lisp macros and Forth metaprogramming capabilities

Every Common Lisp programmer knows that macros are a powerful tool. Common Lisp macros have been used, among other things, to add object orientation on top of Lisp without changing the language specification; read-macros are another construct with mind bending capabilities.

Another program which allows meta-programming is Forth. Forth does it in a slightly different manner, using 'words' and 'generate words'.

I would like to know, from someone who dabbled in both languages, if common lisp macros and forth constructs are comparable in breadth/power: is there something you can do with the former which you can't do with the latter? Or vice-versa?

Of course, I am not talking about Turing-completeness of the two languages: I am talking about metaprogramming capabilities. C is Turing-complete but only a fool would state that C macros are comparable in power to Common Lisp ones.

like image 413
user3750103 Avatar asked Jun 18 '14 09:06

user3750103


1 Answers

In my view, Common Lisp macros are similar to Forth immediate words. (Actually, they are most similar to Lisp reader macros.)

  • They are both procedural macros, i.e. can use full power of the language.

  • They both have access to the source code input.

  • They both can output anything expressible in the language. (E.g. an object-oriented extension in Lisp, or basic control flow constructs in Forth.)

The main difference, perhaps, would be that Forth "macro" input are character strings, while Lisp macros operate on a parse tree.

like image 147
Lars Brinkhoff Avatar answered Sep 22 '22 16:09

Lars Brinkhoff