3 ms·
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 the
by comex 12y ago
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 makes the format less self-documenting (i.e. it may not be immediately apparent whether there can be duplicate keys or not).
http://eli.thegreenplace.net/2012/03/04/some-thoughts-on-json-vs-s-expressions http://eli.thegreenplace.net/2012/03/04/some-thoughts-on-jso...
- kazinator 12y agoThere 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))
- deleted 12y ago[deleted]