34 ms·
Lisp: it's not about macros, it's about READ (2012)
- capitalsigma 10y agoHow is this any different from `eval`, except that lisp's syntax is particularly well suited to being `eval`'d by virtue of being so easy to parse?
- dfox 10y agoEval executes the code, while read only parses it. (incidentally, in lisp, to get same effect as eval in JS/Perl/whatever, you pass the string into read-from-string and then the result from that into actual eval)
- chowes 10y agoI think the main benefit comes from being able to build tools to transform, refactor, etc. your code in very little effort.
- agumonkey 10y agoYes, it raises the abstraction level to allow 'meta' programming as the normal level. Brown university had a chapter on their old PAIP book saying that they voluntarily skipped parsing because it's of no interest, so they used a scheme as a basis for classes.
- lottin 10y agoThe difference is that in most languages eval() parses a character string as code and evaluates it, whereas in Lisp you evaluate language objects (e.g. lists of symbols) rather than strings.
- eyelidlessness 10y agoAs mentioned, `eval` executes code. While macros are generally about manipulating code for eventual evaluation, `read` enables more than that, for instance static analysis. The distinction is also explicit in the term "REPL" (read eval print loop), though many REPLs just pass the input to an interpreter.
- auganov 10y agotldr it's not about macros it's about homoiconicity;
- imglorp 10y agoSurprised op didn't refer to it by name. We can handle big words.
- pramodbiligiri 10y agoPrevious discussion: https://news.ycombinator.com/item?id=3607248 https://news.ycombinator.com/item?id=3607248
- bsder 10y agoOkay, I'll put something heretical out there: Lisp just isn't that much better than our current languages. Sorry. Sure, when Lisp came out it was "advanced". Garbage collection, AST parsing and manipulation, macros, packages, a standard library, modules, homoiconic data structures, etc. However, the good features of Lisp are now normal features of any modern language. And some of those features have gone into the dustbin for good reasons. And some of them (static vs dynamic typing) are personal taste. I remember using Lisp in 1984. It was completely eye-opening relative to the BASIC, assembly, and C I was using up to that point. However, it didn't run well on small machines that we plebians were using back then. So, we plebians moved back to assembly, C and possibly Pascal. Lisp STILL has the same problem on small processors. The embedded world is desperate need of a good language for rapid development, and yet Rust seems to be the only new contender? No offense to Rust, but is that the best we can do? Where is a good dynamic language that is always online and runs in <8K of flash/RAM?
- aidenn0 10y ago> Where is a good dynamic language that is always online and runs in <8K of flash/RAM? Forth immediately comes to mind with those constraints. 8K is an order of magnitude less storage than what the IBM 704 that lisp was first implemented on around 1960 had.
- badcompileerror 10y agoForth is very cool and I agree with what you said, but it is arguable how useful it can be although OP didn't specify that he cared about that (he hinted at what would be a better replacement). I think there are a lot of things you'd have to implement from scratch or attempt to find a collection of words for, which might not work on your Forth. So if you're Chuck Moore then you're good. If not, I'm not sure if the extreme advantages (tiny size, REPL, ~speed, high ceiling/low floor) make up for the fact that you're essentially working in a vacuum.
- mikekchar 10y agoYou can squeeze a FORTH kernel into 2K, so 8K really is quite a luxurious size. I actually wrote FORTH code very early in my career and wrote lots of things you might not imagine doing. For example I wrote a 3D star field animation system for a planetarium in FORTH in the late 80's. I think programmers (especially these days) over estimate how much advantage they get from reusing other packages. Because it becomes so much more difficult to refactor your code when you can't change some interfaces, you may end up with more (or at least more complicated) code than if you wrote something that is tailor made. In languages where you have a lot of facilities available, it is one of the hardest decisions to get right. When you are forced to write everything yourself, then it's easier to get the choice right ;-)
- dogfishbar 10y agoHa! LISP macros and read both work because of a very simple bug in the original definition of LISP. Don't even bother with it or Scheme or Racket --- they're all utterly broken and needlessly confusing!
- ZenoArrow 10y agoWhich bug are you referring to?
- dogfishbar 10y agoThe definition of quote is broken. I personally explained it to John McCarthy. He agreed.
- sesquipedalian 10y agoCare to elaborate?
- dogfishbar 10y agoOK, you asked! LISP was originally developed as a language for writing recursive functions of symbolic expressions (S-expressions). It was roughly based on lambda calculus, the language developed by Alonzo Church. But roughly is the key word. S-expressions are given by the context-free grammar: S ::= A | [] | (S . S) One can write data structures this way and lists by using the abbreviation: (S1 . (S2 . ( ... ( SK . ()) ...))) == (S1 S2 ... SK) The terms that manipulated these S-expressions were called M-expressions. They were first-order terms. McCarthy's key idea was to use a conditional in conjunction with a label form to define recursive functions (in the service of various AI applications). The M-expressions were defined roughly as follows: M ::= S | x | if[M; M; M] | f[M; ...; M] f ::= lambda[[x1; ...; xn]; M] | label[g; M] (I'm not 100% confident that I remember the exact details on this syntax but the idea is correct!) McCarthy wanted to show that his new language was Turing-complete. So he wanted to exhibit a universal function APPLY (derivable from f above) such that for any function f and arguments M1; ...; Mk such that f[M1; ...; Mk] evaluates to S-expression S, well, given a representation of f[M1;...;Mk], lets call it, hat(f[M1;...;Mk]), well APPLY[hat(f); hat(M1);...;hat(Mk)] would evaluate to hat(S). This is pretty much a standard formulation of the recursion-theoretic argument. In order to close the sale, McCarthy had to exhibit such an APPLY and also the hat(.) function. Sadly for all of us LISP lovers (!) he botched the definition of hat(.)! It left people utterly confused for 30+ years. Such a shame. Fixed a couple of typos. Sorry! Fixed one other typo! It's been a while...
- dschiptsov 10y agoNope. Lisp is about extending itself with new special forms and embedding DSLs into itself ( SETFs, LOOPs, DEFSTRUCTs, even whole CLOS, to name a few). With macros one could introduce new special forms, which differs from mere high order procedures by having its own evaluation rule implemented as a macro. In particular some of its arguments could be left unevaluated before application, which gives one laziness and other nice things (for an ordinary procedure, according to the general evaluation rule, all the arguments will be evaluated in order before application of the procedure). Lisp isn't about hipsters. It is about fundamentals of programming. Here is a simple illustration: http://karma-engineering.com/lab/wiki/Bootstrapping8 http://karma-engineering.com/lab/wiki/Bootstrapping8
- DalekBaldwin 10y ago> So what are macros? All they are is read packaged up nicely into formal system. Macroexpansion happens during a different phase altogether, and would work exactly the same even if programmers had no access to the reader at all. The reader transforms an unstructured character stream into structured data, while macroexpansion transforms already-structured data into different structured data. (Read-macros are another story, but it's clear they're not what the author had in mind.) Too often these kinds of posts only add to the confusion they're attempting to clear up.
- beders 10y agoIt's all about the read. But a different one. The human ability to read code. You can write very dense code in Lisp (and the author makes the point several times on how little lines of code you need for this vs. that). However, who's going to read that and understand it? Especially problematic with code that looks like a regular s-expression but has a completely different semantic thanks to macros. Lisp is great if you want to write code. Reading it...you mileage may vary
- mbrock 10y agoYeah, it really depends. Powerful language features let you write code that is extremely clear since you can shape the language to your domain. It's not just for making obfuscated abstractions or code golf. Since this topic comes up a lot, I'm often curious about what experiences people have with reading and writing Lisp code... My own experiences tell me that almost all code, in all languages, is difficult to read and understand—and I often long for Lisp-like features to help me make programs more clear.
- junke 10y ago> a completely different semantic thanks to macros. Most of the time, you use functions. Sometimes, you need to define "WITH-X" macros or "DO-Y" iterators, or a custom "DEFINE-Z" to shorten declarations (I tend to use macrolet for that). (defun rgb (r g b) (format nil "#~@{~2,'0x~}" r g b)) (defparameter *background* (rgb 10 10 30)) (defparameter *foreground* (rgb 90 90 255)) (with-html-output-to-string (s) (:html (:head (:title "Hello") (:style :media "screen" :type "text/css" (str (cl-css:css `((:body :background-color ,*background* :color ,*foreground*))))) (:script :type "text/javascript" (str (ps:ps (setf (ps:@ window onload) (lambda () (alert "Message"))))))) (:body (:h1 "Heading")))) The above is an example of what I consider a heavy usage of macros, and yet somehow I find it readable. (the output is here: http://pastebin.com/raw/S5cb5ufC http://pastebin.com/raw/S5cb5ufC)
- junke 10y ago
- golergka 10y agoFantastic article, but I still don't quite understand what kind of real-world tasks on typical projects become easier with AST manipulation. After all, how often do you write a debugger?
- mordocai 10y agoTheoretically every project is easier with AST manipulation due to being able to create a high level DSL that directly maps to your problem. In practice, I think good DSLs are hard to make and so most projects never get the full benefit. I don't have any production lisp experience though(i'm a hobbyist lisper) so I could definitely be wrong there
- sedachv 10y ago> Fantastic article, but I still don't quite understand what kind of real-world tasks on typical projects become easier with AST manipulation. After all, how often do you write a debugger? A good example comes from Python: decorators had to be added as a special extension in 2003 (https://www.python.org/dev/peps/pep-0318/ https://www.python.org/dev/peps/pep-0318/) and the `with` statement in 2005 (https://www.python.org/dev/peps/pep-0343/ https://www.python.org/dev/peps/pep-0343/). Both are very useful for web programming, but do not need PEP committees and years of work when you have macros (examples: https://github.com/vsedach/cliki2/blob/master/src/accounts.lisp#L213 https://github.com/vsedach/cliki2/blob/master/src/accounts.l... https://github.com/vsedach/cliki2/blob/master/src/wiki.lisp#L155 https://github.com/vsedach/cliki2/blob/master/src/wiki.lisp#...)
- mdpopescu 10y agoAn article that mentions abstracting things and then immediately uses CAR and CDR...
- wallforbidmen 10y agoI can say an image with sbcl (lisp implementation) with a load of functions and libraries. I can't save an image with python, ruby and javascript.
- naveen99 10y agoYou can checkpoint at a lower level https://en.m.wikipedia.org/wiki/Application_checkpointing https://en.m.wikipedia.org/wiki/Application_checkpointing or using a virtual machine. But in practice it's not that useful to checkpoint a running program because of external state. You can't checkpoint sockets when they are connected to some other machine.
- rpazyaquian 10y agoThis article helps explain the how, but doesn't really cover the why. Coming from a functional programming perspective (Ruby, Elixir, Clojure [yes I know]), I don't see where macros become useful or anything more than a curiosity outside of a couple situations: 1. Implementing a DSL, which is its own beast and is more often a bad idea than not; or 2. Actually, I can't think of anything else. Granted, I might be spoiled by modern conveniences already implemented in Clojure, such as cond. It's just hard to see what macros can give me that I can't accomplish with functions. If code is data, then aren't functions, which operate on data, already what we're looking for?
- nickpsecurity 10y ago"Implementing a DSL, which is its own beast and is more often a bad idea than not" Those using languages supporting that disagree. Let's assume the DSL is syntax embedded in a language like LISP or Haskell so you don't get stuck in the DSL. Essentially, there's not much difference between a DSL and library in terms of what's required to use it. You still learn what you type to achieve a certain effect. You still can use rest of language. Main difference is both (a) matches the problem domain more closely and (b) limits you to expressing just what you need to solve the problem. These are why the BASIC-oriented 4GL's were quite successful. LISP, too, given it's also easy to process. REBOL & recently Red take it further. Kay et al are working wonders where a whole system is expressed in readable code that's a tiny fraction of code in a non-DSL system. The combo of easy-to-modify language and DSL toolkit (esp Racket or Red) also leads to methodologies like this one from sklogic where it's easy to do components with a series of DSL's that are thrown together & many pieces autogenerated: "The method is very simple: * describe the problem in plain English (maybe with some diagrams) * iterate it a few times until you have a syntax you think is unambiguous enough * strip this syntax from all the sugar you just introduced in order to define an AST * find DSLs in your toolbox that are potentially close to the one you're building and cherry-pick the necessary language components from them * write a sequence of very simple transforms that would lower your source AST into a combination of the parts of the ASTs of the target DSLs of your choice * Done. An efficient DSL compiler is ready, with a language designed as closely to your current view of the problem domain as possible. * If you later find that your DSL is inadequate and your understanding of the domain was insufficient, than just start over again, this entire process is so cheap and simple that it does not really matter." It also helps with verification. You can define DSL's for specific types of OS components or app software that make correct-by-construction easier. CertiKOS is doing that: http://flint.cs.yale.edu/certikos/publications/ctos.pdf http://flint.cs.yale.edu/certikos/publications/ctos.pdf http://flint.cs.yale.edu/certikos/certikos.html http://flint.cs.yale.edu/certikos/certikos.html
- thelazydogsback 10y agoIt always struck me as odd that many programming languages have standard printers that will output any data structure expressible in the language, but they don't have accompanying readers - this forces one to roll one's own or use a completely separate serialization technique. This is useful even when not in a homoiconic language for day-to-day tasks.