4 ms·
I used to like Lisp's homoiconicity. These days, I'm not even sure the concept makes any sense? What is it even supposed to mean? Lisp has (simple) syntax fo
by eru 3y ago
I used to like Lisp's homoiconicity. These days, I'm not even sure the concept makes any sense? What is it even supposed to mean?
Lisp has (simple) syntax for creating literals and writing code. But when you inspect your data from the inside of Lisp, they don't have any more (or less) relation to that syntax than eg Python does.
Eg no modern Lisp stores a function as an S-Expr of its syntax. You get an opaque piece of data that you can do certain operations on, just like in eg Python or Haskell.
Lisp using S-Expressions is an interesting syntax choice, but that's it. It doesn't by itself have any deeper impact on the language. (Of course, socially it does have a deeper impact, because it makes it simple for language users to write macros that look exactly like built-in parts of the language, instead of bolted on.
But that's all in human brains and social interactions, nothing fancy going on here on a technical level.
If you wouldn't mind much more tediousness, you could implement seamless macros in almost any language as a pre-processing step.)
- thom 3y agoWell sure, if you hand wave away the main benefit of sexps then there aren’t many benefits.
- eru 3y agoOh, S-Expressions are great for certain kinds of things. I'm just having a problem with the concept of homoiconicity being touted as both coherent, and something that goes deeper than the very surface syntax level.
- deredede 3y agoOf course, compilers exist, so in a sense all code is data. Besides the social stuff, S-expressions make it orders of magnitude simpler to build the infrastructure required to support seamless macros (just look at procedural macros in Rust for a recent example)
- brabel 3y agoI can't imagine someone who has ever written a Lisp macro coming away thinking "there's nothing special about s-expressions". Honestly, sounds like you're commenting on something you have absolutely no knowledge of.
- eru 3y agoGreat ad hominem! I have used Scheme, Racket and Common Lisp (SBCL) since the 1990s (before mostly switching to the ML family of languages). I read 'On Lisp' cover to cover. I know what I am talking about. You can still disagree with me, of course. But don't do it of the basis of me not having used enough Lisp. S-Expressions make it easier to write macros that blend seamlessly into the host language. But there's no magic to them (compared to macros you could do as pre-processing in any other language) beyond that. S-Expressions are a neat syntax. But homoiconicity is a weird concept; if it is coherent at all, it only pertains to the surface syntax level of the language.
- lisper 3y ago> I know what I am talking about. Sorry, but you manifestly don't: > there's no magic to them (compared to macros you could do as pre-processing in any other language) The "magic" of s-expressions is that they make it easy to operate on the source code of a program as a hierarchical data structure (i.e. as an AST) rather than as text. That turns out to be an extremely powerful lever. It is is one of the reasons Lisp has lasted as long and been as influential as it has. I'm sorry if this sounds like an ad-hominem, but if you think there is "no magic" to S-expressions the most likely explanation is that you don't really understand them. There is nothing comparable in any other language. There's a reason that they are still in use today. Indeed, there is a reason that they keep getting re-invented again and again. HTML, XML, and JSON are all (badly) reinvented s-expressions. > homoiconicity is a weird concept No, it isn't. It is simple and straightforward: homoiconicity is where the primary representation of programs is a data structure in a primitive type of the language itself. It isn't any weirder than (say) recursion. If you think it's weird that is more evidence that you don't actually understand it.
- 3y ago
- medo-bear 3y ago> Eg no modern Lisp stores a function as an S-Expr of its syntax. You get an opaque piece of data that you can do certain operations on, just like in eg Python or Haskell. In Common Lisp (defun times-two (x) (\* x 2)) (print (function-lambda-expression #'times-two)) > (LAMBDA (X) (BLOCK TIMES-TWO (* X 2))) What would you do in Python or Haskell?
- eru 3y agoSorry, I should have been more precise. Yes, Common Lisp stores the s-expression. And you can get at it by applying an operation (in your case, `function-lambda-expression`) to some opaque handle. The function itself isn't stored as its s-expression. Neither does any reasonable interpreter work by walking the s-expression. (Doing so would be very slow.)
- medo-bear 3y agoYes, I'm having trouble getting precisely to what you are talking about. Do you mean something like SBCL compiling to machine code, for example? If so, how does that affect using Common Lisp to implement some DSL, in the sense that it takes away the power of s-expressions for achieving this?
- eru 3y agoI am saying that S-Expression aren't any kind of 'primary' representation of your program in a modern Lisp. Especially a compiled one. S-Expressions (or more precisely, the AST) is just one representation amongst many that's in use during the different stages of the compilation process. They are still a great syntax choice for the kind of languages that Lisps are. Totally agreed. My point was that homoiconicity is a surface level property that you could bolt on to eg C or Cobol, via a simple pre-processor, and it wouldn't change those languages much. Just like you could bolt on using Python-style significant indentation to C or Fortran or Lisp, and it wouldn't change those languages much. (Apart from changing their syntax, of course.)
- 3y ago
- soegaard 3y ago> I used to like Lisp's homoiconicity. These days, I'm not even sure the concept makes any sense? What is it even supposed to mean? You are not alone: http://calculist.org/blog/2012/04/17/homoiconicity-isnt-the-point/ http://calculist.org/blog/2012/04/17/homoiconicity-isnt-the-...
- eru 3y agoThanks! That blog post expresses much more clearly what I am trying to say. Btw, it seems you could implement a 'read' that also recognises Python/Haskell-style significant indentation in addition to parens only. That would be an interesting elaboration of the point in the post.
- medo-bear 3y ago> Parentheses make it unambiguous for the expander to understand what the arguments to a macro are, because it’s always clear where the arguments begin and end. It knows this without needing to understand anything about what the macro definition is going to do. Imagine trying to define a macro expander for a language with syntax like JavaScript’s. Are you sure this blog post is making the point you have been trying to make? Here is blog post that explores writing C in s-expressions. https://itnext.io/what-makes-lisp-unique-8a0576b42293 https://itnext.io/what-makes-lisp-unique-8a0576b42293
- lispm 3y ago> Eg no modern Lisp stores a function as an S-Expr of its syntax Many Common Lisp implementations do that, especially those with an interpreter: SBCL (yes, SBCL has an interpreter, too -> one can tell SBCL to use an interpreter), LispWorks, a Lisp Machine, Allegro CL, CLISP, ... LispWorks: CL-USER 18 > (defun foo (a b) (* a (let ((a (* a a))) (break) (+ b (expt a b))))) FOO CL-USER 19 > (foo 1 2) Break. 1 (continue) Return from break. 2 (abort) Return to top loop level 0. Type :b for backtrace or :c <option number> to proceed. Type :bug-form "<subject>" for a bug report template or :? for other options. CL-USER 20 : 1 > :n Call to INVOKE-DEBUGGER CL-USER 21 : 1 > :n Call to BREAK CL-USER 22 : 1 > :n Interpreted call to FOO You'll see that LispWorks says that FOO is running using the s-expression interpreter. Next we'll have a look at this s-expression: CL-USER 23 : 1 > :lambda (LAMBDA (A B) (DECLARE (SYSTEM::SOURCE-LEVEL #<EQ Hash Table{0} 82200D6A93>)) (DECLARE (LAMBDA-NAME FOO)) (* A (LET (#) (BREAK) (+ B #)))) The list is a value, which we can pretty print: CL-USER 24 : 1 > (pprint *) (LAMBDA (A B) (DECLARE (SYSTEM::SOURCE-LEVEL #<EQ Hash Table{0} 82200D6A93>)) (DECLARE (LAMBDA-NAME FOO)) (* A (LET (#) (BREAK) (+ B #)))) If you would modify this s-expression in the debugger, the program is directly changed.