3 ms·
One major Lisp thing I've been musing about lately... C programs don't understand other C programs. Of course the C compiler itself is an exception but in gene
by dreamcompiler 6y ago
One major Lisp thing I've been musing about lately...
C programs don't understand other C programs. Of course the C compiler itself is an exception but in general you don't expect to be able to feed your compiled C .exe another C source file and do anything useful with it. That's crazy talk. Likewise with Java or C++ or Fortran or...
But Lisp programs understand Lisp programs. The compiler is always available even at runtime. And not just the compiler but the lexical analyzer, the parser, and the evaluator too. Output is also easy: You can write a Lisp program that builds Lisp data structures dynamically in memory and then prints them to a text file as Lisp source code. Which you can later read back in and execute as a program.
This kind of thing is so natural in Lisp that it opens up whole new ways to think about code. So why is this useful?
One example: In many programs you need configuration files. So you write configuration files for C programs in XML or yaml or JSON and then use a library function to parse them. Why don't we write configuration files for C programs in C syntax? Well, because C syntax is rather inconvenient for this purpose and because there aren't many C libraries that parse C syntax. Besides, how do you handle includes, compiled libraries, macros, linking, etc? Again, crazy talk.
Not so in Lisp. Lisp syntax is very convenient for configuration files (because it's s-expressions) and the parser is built-in. So you write configuration files in Lisp and parse them at runtime! If you change a setting at runtime, it takes not much more than one line of code to automatically print a new configuration file. Which you can then tweak with a text editor if you so desire.
Another example: Macros. Macros use this same principle but there's no external file involved. Macros change Lisp's syntax so you can write more advanced programming constructs and translate them to basic Lisp at compile time. It's the same idea as Typescript-- Typescript is a Javascript program that converts a type-safe version of Javascript into basic Javascript. But Typescript was a one-off major project--like building a compiler. In Lisp the toolkit for source-to-source translation is built-in and Lisp programmers use it every day.