Live data from Hacker News

Why Lisp?

blog.rongarret.info

211–220 of 248 posts

Re: Why Lisp?

#211
post #185
post #111

Earlier quoted context omitted.

> XML was never designed as a data serialization format. It's a markup language. Those two things are not mutually exclusive. > Likewise, JSON is a subset of a general-purpose programming language's literal notation that happened to be very fast to parse in a browser by virtue of the browser implementing that language. That's true. That is not in conflict with anything I said. > The problem is that there's no one-siz…

One downside to S-exprs compared to, say, JSON: they do not have direct support for unordered mappings (hash tables, dictionaries, whatever you want to call them). You can represent them as trees, but basically every language these days (including, of course, Lisps) has a mapping type as a core concept; requiring the user to figure out what parts of the input data should be converted to that type is annoying, and mak…

There is no "ISO Standard Sexp" way to have a hash table.

However, Sexps easily support hash tables.

Example:

  $ txr -p "(let ((x (hash))) (set [x 'a] 1) (set [x 'b] 2) x)"
  #H(() (b 2) (a 1))
Here I have chosen a "hash capital H" syntax for hash tables. The first part () has the hash attributes (there are none, so it's an eql-equality-based hash table, with strong keys and values). Then, entries consisting of two element list pairs give the keys and values.

This hash prefix notation builds on existing Lisp concept of using simple prefixes to distinguish various kinds of objects. In Common Lisp we have:

  #(1 2 3)     ;; this is a vector
  #C(3.0 4.9)  ;; this is the complex number 3.0 + 4.9i
The scanning is very simple: you just recognize the prefix like #C( or #( and then recurse into the scanner for list elements that stops at a closing parenthesis.

No whitespace is allowed: it cannot be # (1 2 3) or # C (3.0 4.9).

TXR Lisp above not only reads back the hash notation, so it can be used as a literal, but allows backquoting over it. We can splice keys and values into the syntax to produce a hash table:

  $ txr -p "(let ((keys '(a b c)) (vals '(1 2 3)))
               ^#H(() ,*(zip keys vals)))"
  #H(() (c 3) (b 2) (a 1))

Re: Why Lisp?

#212

I actually created a hybrid of JSON and Lisp called Geneva [1]. It was a fun weekend experiment. I totally relate to how the author feels [1] https://github.com/smizell/geneva

Early versions of wat-js used ["the", "same", "syntax"]. You might find it interesting. https://github.com/manuel/wat-js

Ah, very interesting! Thanks for the link.

Re: Why Lisp?

#213

Earlier quoted context omitted.

One thing to note is that you're using the macro or lambda to delay evaluation. In a lazy-by-default language, that's unnecessary (which is a part of why macros are less useful in Haskell).

There's a class of things that don't require macros in Haskell, but I don't think that means macros are less useful in Haskell. There are plenty of things you might want them for, like generating new definitions.

I certainly didn't want to imply that macros were not useful in Haskell. Just that there's good reasons you see them less often.

Depriving a Haskell programmer of the ability to define new macros is going to hurt a whole lot less than depriving a Lisp programmer of the same.

Re: Why Lisp?

#214
post #178

Earlier quoted context omitted.

One thing to note is that you're using the macro or lambda to delay evaluation. In a lazy-by-default language, that's unnecessary (which is a part of why macros are less useful in Haskell).

True. They are still useful enough for Template Haskell to exist.

For sure.

Re: Why Lisp?

#215

(I have a subtle optimization for S-expression syntax)(I am surprised nobody ever thought of it)(When S-expressions are in a sequence use an extra (special) delimiter plus the regular token separator to separate expressions)(Maybe use dot? (period I think some call it)) Like so. I think it could catch on. And you get rid of so many round bracket block delimiters (at least for S-expressions on the same level. for nest…

You'll love my binding-block macro then. You can find it here:

https://github.com/rongarret/ergolib

Re: Why Lisp?

#216
post #185
post #111

Earlier quoted context omitted.

> XML was never designed as a data serialization format. It's a markup language. Those two things are not mutually exclusive. > Likewise, JSON is a subset of a general-purpose programming language's literal notation that happened to be very fast to parse in a browser by virtue of the browser implementing that language. That's true. That is not in conflict with anything I said. > The problem is that there's no one-siz…

One downside to S-exprs compared to, say, JSON: they do not have direct support for unordered mappings (hash tables, dictionaries, whatever you want to call them). You can represent them as trees, but basically every language these days (including, of course, Lisps) has a mapping type as a core concept; requiring the user to figure out what parts of the input data should be converted to that type is annoying, and mak…

[deleted]

Re: Why Lisp?

#217
post #161

Earlier quoted context omitted.

One that helped some Java friends understand is passing blocks of code, but still having it look like just writing code. Imagine instead of try/catch/finally, a transaction/commit/rollback in Java: transaction { // everything in here is in one transaction } commit { // do stuff if the commit is successful } rollback { // do stuff if we rollback } All the try's and catch's can be stuff into the macro. It can be made t…

I find this argument pretty unconvincing too. >For all practical purposes, you can't add that to Java. You'll always have to wrap up your transactions in boilerplate. Aren't you wrapping the lisp code in boilerplate when doing the macro too? This appears to be the same as your other example. Java has lambda expressions (since Java 8) that could do this. If you are wrapping it in a macro, how is it different than wrap…

Agreed, I'm still unconvinced for the same reasons. In Scala or Haskell (or any language which supports first-class functions and lazy evaluation, I guess) the "transaction" example is easily done.

Re: Why Lisp?

#218

The article doesn't discuss macros, which is one of the answers to "Why Lisp?" I didn't "get" macros until I read a footnote in the (freely available) book Practical Common Lisp . In chapter 7, it introduces the `dolist` macro. DOLIST loops across the items of a list, executing the loop body with a variable holding the successive items of the list. This is the basic skeleton (leaving out some of the more esoteric opt…

The book you linked has examples of macros being used for writing a music database, parsing binary files, a unit testing framework, and several other things I haven't reached yet. Those are much better examples of macros than basic control structures. The 'dolist' form is a toy example.

Re: Why Lisp?

#219
post #147
post #143

Earlier quoted context omitted.

Because macros let you control code in a way libraries don't^1. Java now has this way to iterate over a collection of things: for(String exclamation: exclamations) { System.out.println("I yell " + exclamation + " at you"); } But this was only added in 2004!^2 So for almost 10 years, you had to manually iterate over stuff. How would you implement this as a library? Well, you could write a function that lets you write:…

Agreed about your Java example. However, for the purpose of this discussion, let's assume we're talking about modern, well-designed languages without ugly kludges and with access to nice features such as lazy evaluation and real closures. > Macros can be viewed as libraries that act on the language itself. There isn't a difference between language-level features and "library functions" in a language with macros. I si…

From Peter Seibel's "Practical Common Lisp":

DOLIST is similar to Perl's foreach or Python's for. Java added a similar kind of loop construct with the "enhanced" for loop in Java 1.5, as part of JSR-201. Notice what a difference macros make. A Lisp programmer who notices a common pattern in their code can write a macro to give themselves a source-level abstraction of that pattern. A Java programmer who notices the same pattern has to convince Sun that this particular abstraction is worth adding to the language. Then Sun has to publish a JSR and convene an industry-wide "expert group" to hash everything out. That process--according to Sun--takes an average of 18 months. After that, the compiler writers all have to go upgrade their compilers to support the new feature. And even once the Java programmer's favorite compiler supports the new version of Java, they probably still can't use the new feature until they're allowed to break source compatibility with older versions of Java. So an annoyance that Common Lisp programmers can resolve for themselves within five minutes plagues Java programmers for years.

Re: Why Lisp?

#220
post #172

Earlier quoted context omitted.

>> XML was never designed as a data serialization format. It's a markup language. > Those two things are not mutually exclusive. I beg to differ. I just replied to someone else about this: https://news.ycombinator.com/item?id=9509110 I agree with your last paragraph, though. There is a timelessness about S-expressions. As a side point, I would add that the distinction between strings and symbols is important, and nei…

The comment you were responding to got deleted, which makes it a little hard to figure out what's going on there. But I am completely nonplussed at your assertion that markup and serialization are mutually exclusive. There is a 1-to-1 correspondence (actually multiple 1-to-1 mappings) between XML and S-expressions, so whatever you can do with sexprs you can do with XML modulo some trivial transformation. The ONLY dif…

Obviously you can encode S-expressions in XML (including symbols). But you have to add additional structure to do it. The point is that XML, following the markup metaphor, doesn't work this way out of the box. And, in fact, I've never seen anyone (except, I guess, you) go to the trouble of making all the distinctions in XML, such as the string/number distinction, that S-expressions make -- and I have seen people get into trouble for failure to do this.

It's a psychological/sociological point rather than a technical one, but metaphors matter in design.

Post reply on HN