4 ms·
I've on more than one occasion used Lisp's "everything is data" idea to build little domain-specific languages in JSON. One of the easiest places to start is wi
by drostie 12y ago
I've on more than one occasion used Lisp's "everything is data" idea to build little domain-specific languages in JSON. One of the easiest places to start is with representing HTML this way; you can e.g. start to write the Python or JS code:
["table", ["tbody", ["tr", ["td class=first", 1], ["td", 2]]]]
instead of:
<table><tbody><tr><td class="first">1</td><td>2</td></tr></tbody></table>
A really early attempt at this is: https://github.com/drostie/d.o/blob/master/build.js https://github.com/drostie/d.o/blob/master/build.js but I haven't maintained it in a while.
If you're interested in language design, there appears to be a "little gap" which means a lot between a Lisp data structure and an XML data structure: In XML's view, every list's first element is a string; in Lisp's, it can be anything. This little gap actually corresponds to one of the interesting habits which has disappeared since compilers took over our assembly-writing: it used to be the case that programmers would sometimes compute an instruction code for the next instruction; similarly in Lisps you sometimes (but not too often; it's confusing!) compute a function for your next operation.
If that confuses you because you're not good with Lisp, it's equivalent in Python or JS is:
(f(x, y, z))(a, b, c)
so that you figure out which function to call and then immediately call it on some parameters. It's not necessarily a difference in expressiveness, as the XML-style just can add an atom 'call' which expresses the above as:
call(f(x y z) a b c)
but it does seem to correspond to some deeply different philosophy for the system.
- davexunit 12y agoYour JSON snippet is reminiscent of SXML, a format for describing XML/HTML as s-expressions. The difference being that, using quasiquote and unquote, Lisps are full-fledged templating languages. Here's a simple template written in Scheme: (define stuff '("foo" "bar" "baz")) `(ul (@ (class "foo")) ,@(map (lambda (thing) `(li ,thing)) stuff)) Output: (ul (@ (class "foo")) (li "foo") (li "bar") (li "baz")) Fun stuff!
- zwp 12y ago> representing HTML You may like Clojure's Hiccup: https://github.com/weavejester/hiccup https://github.com/weavejester/hiccup http://www.rkn.io/2014/03/13/clojure-cookbook-hiccup/ http://www.rkn.io/2014/03/13/clojure-cookbook-hiccup/ These representations underline how anomalous <IMG...>, <BR>, etc are in "standard" HTML.