6 ms·
S-expressions
- lttlrck 14y agoThanks! 30 seconds of reading and I have at least a basic understanding.
- octo_t 14y agoParsing S-expressions is easy: Dijkstra solved it in a really nice way with the Shunting Yard Algorithm.
- slurgfest 14y agoThank you for this post, because it made me curious enough to look up Dijkstra's views on LISP. (It seems he credited it for some innovations but had a lot of complaints about its early design and docs, and favored ALGOL family design decisions)
- krickle 14y agoI thought that was for infix expressions with order of operations?
- brudgers 14y ago"Stay tuned for follow-up posts." For those interested, there was follow-through on the promise to follow-up: https://igor.io/ https://igor.io/
- sea6ear 14y agoThe follow-up posts look really interesting. Looks like they go through the complete implementation of a lisp system including macros using PHP as the host language.
- mwexler 14y agoWhat's great about this is not only that it's a good quick summary, but it left me wanting more. That means I actually feel like I was getting somewhere with it, instead of it being instantly forgotten.
- splawn 14y agoWas the use of dict and list necessary? This is an actual question, not a rhetorical criticism. I have recently been trying to learn common lisp.
- enduser 14y agodict is not a function or macro in common lisp, so the example is fictitious. in CL one would construct an A-list or a P-list (both cons-based ways of expressing a small map). see http://www.gigamonkeys.com/book/beyond-lists-other-uses-for-cons-cells.html http://www.gigamonkeys.com/book/beyond-lists-other-uses-for-... for more info on that. the need for (list ...) would depend on the definition of dict.
- DasIch 14y agoUsing dict and list here preserves the dynamic nature of JSON. If you have some sort of schema - which in reality you probably would have, even using JSON - that implies dict and list you wouldn't need to use those.
- rdtsc 14y agoIt is if you want to differentiate between a list of lists and a map. Maps are not natively supported in s-expressions. So there is a need to create a sub-language to handle special data structures. I have done this as well. But I use a special namespace '#dict' for example. So it would be (#dict (k1 v1) (k2 v2)) This way there is less of a chance of collision between data and this sub-language. It also allows for extension, so for example instead of dict you can use #classname to say persist a perticular object of a given class name (json doesn't allow this, there would have to add a '__classname__' key to the object for example. Another way of doing it is using a flat list and just using an alternation with keys being prefixed by some special character: (:k1 v1 :k2 v2) I think Clojure and some lisps do that...
- groovy2shoes 14y agoClojure s-expressions actually have syntax for vectors, maps, and sets (in addition to the ones mentioned in TFA): {:k1 v1, :k2 v2} ; curly braces for maps [v1 v2 v3] ; square brackets for vectors #{v1 v2 v3} ; pound-curly for sets Clojure's reader also provides some mechanisms for adding readable values, which is awesome for serialization and interchange. There's more info here: http://clojure.org/reader http://clojure.org/reader
- manaskarekar 14y agoA highly recommended read from defmacro. http://www.defmacro.org/ramblings/lisp.html http://www.defmacro.org/ramblings/lisp.html Other articles on the site are very nice as well: http://www.defmacro.org/ http://www.defmacro.org/
- ScottBurson 14y agoYes, it is a great article, but it saddens me that we have to invoke XML to explain S-expressions rather than the other way around. S-expressions have been around since 1958! They are maybe the best reason that everyone should learn Lisp even if they don't wind up using it much, just so they know there are problems it already solved long ago, and they don't need to reinvent the wheel.
- MichaelGG 14y agoWhy are the keywords written with a quote? > (keywords '(useless microframework academic swag)) Shouldn't it be a list with strings or something? What if the keywords were complex structures not just strings?
- nialo 14y agoif it's not quoted the list will be treated as an expression, and evaluate "Useless", which is probably not what you want.
- MichaelGG 14y agoRight so instead you'd write: keywords (list "a" "b") ? Which would let you also do complex things like (list (person "bob") ...)
- deleted 14y ago[deleted]
- RaphiePS 14y agoSee my comment above. The core difference is that (list ...) evaluates what's inside. So (list (+ 1 2)) returns (list 3). ' doesn't evaluate anything. So '((+ 1 2)) returns '((+ 1 2)).
- RaphiePS 14y agoThe only Lisp I know is Racket (basically Scheme), so take the following with a grain of salt. '(foo bar baz) is almost the same thing as (list foo bar baz). However, ' also makes the contents of the list into symbols, not variable references. Basically, '(foo bar baz) returns (list 'foo 'bar 'baz), not a list containing the values of the variables foo, bar, and baz. Symbols aren't much like strings. They're immutable, and actually just refer to a unique number, not characters. I hope that sort of makes sense!
- nivertech 14y ago'(foo bar baz) is sugar for (quote (foo bar baz)) list function unlike quote will evaluate it's arguments, i.e. user=> (quote (a b c)) (a b c) user=> '(a b c) (a b c) user=> (list a b c) CompilerException java.lang.RuntimeException: Unable to resolve symbol: a in this context, compiling:(NO_SOURCE_PATH:1) user=> (list 'a 'b 'c) (a b c)
- chimeracoder 14y ago> Side note: Wouldn't it be awesome if composer supported composer.sexpr files natively, so that we would no longer have to write JSON? No, not really. Why the quick dismissal? I find s-expressions much easier to work with than JSON.
- igorw2 14y agoLet me clarify the reason for this. Many people asked for yaml support in composer, and it was shot down. Here's why: Once you start supporting many formats you lose interoperability, as any tooling now needs to support all formats. That is the main reason why composer will not support sexpr.
- ecmendenhall 14y ago> Side note: Wouldn't it be awesome if composer supported composer.sexpr files natively, so that we would no longer have to write JSON? If you, too, think this would be awesome, check out extensible data notation: https://github.com/edn-format/edn https://github.com/edn-format/edn.
- billpg 14y agoYou could write code in XML and JSON too, you just need to define meanings. [add][item]1[/item][item]2[/item][/add]
- TeMPOraL 14y ago> You could write code in XML and JSON too And many do (e.g. Ant), but doesn't that sadly look like reinventing the wheel?
- jhrobert 14y agoIf you love the expressiveness of S-expressions but hate the super noisy parenthesis, check the sugar version, called "sweet-expressions". http://readable.sourceforge.net/ http://readable.sourceforge.net/
- martinced 14y agoYou have to love the intellectual dishonesty of that site you linked to: they quote Paul Graham whose not just a big Lisp dialects advocate but also probably partly responsible for the regain of interest in Lisp dialects... Yet they quote pg as if he ever said that Lisp source code were ugly. Regarding the "noisyness", I really think that the one and only place that you can really criticize is the closing of the outermost method. And any text editor worth its grain of salt can be programmed to automagically collapse these closing parentheses. Hence preserving the eyes of non-lispers. Also, when you're using something like paredit or subpar you hardly ever have any issue of non-matching parentheses. I'm really surprised that people are still whining about that instead of trying to focus on the bigger picture: the benefits that homoiconicity brings to the table.
- orthecreedence 14y agoAgreed. You don't hear people bitching about all the {}'s in C and Java, but when lisp comes up you'd think parens were responsible for the plague. Get a real editor and deal with it. Once you write your first macro, you'll realize how stupid all the whining was and why homoiconicity is in many aspects superior to other syntaxes without sacrificing much at all.
- chc 14y agoWell, a lot of it is that Lisp uses parens for everything while most other languages use a variety of syntactic tools. For example, declaring a bunch of variables in Lisp: (let ((a 1) (b 2) (c 3)) (format t "They are ~D, ~D and ~D~%" a b c)) The same thing in C int a = 1; int b = 2; int c = 3; printf("They are %d, %d and %d\n", a, b, c); Lisp used parens around the whole construct, as well as around the assignment list, around each individual assignment and to do the printing. C completely eschews some of these parens and uses = and ; to fill essentially the same role as the others. Only `printf()` keeps the parens. In short, C has lots of different syntactic symbols, while Lisp has relatively few. To illustrate further, Clojure gets the "OMG parens" complaint a lot less than most Lisps because it consciously shies away from the traditional parens-everywhere look of Lisp. For example, the above code in Clojure: (let [a 1 b 2 c 3] (printf "They are %d, %d and %d\n" a b c)) Still more parens than C, but the difference is marked — the assignment block is clearly set apart by syntax, and the assignments are paired by juxtaposition rather than by grouping into lists. Parens are used relatively sparingly. This isn't to say that this is really a problem, but the idea that parens in Lisp are equivalent to brackets in C isn't really right either. Parens in Lisp are used in place of nearly every bit of syntax in C, from braces to equals signs to semicolons.
- japaget 14y agoNear the front of the main article is "(sexpr lexer reader eval forms special-forms macros walker)". Each word in the above parenthesized expression links to another article in the series.
- juan_juarez 14y ago> I want to know how to parse them in PHP. Step one : Stop using PHP.
- informatimago 14y agoSee also Rivest's Sexps: http://people.csail.mit.edu/rivest/Sexp.txt http://people.csail.mit.edu/rivest/Sexp.txt