Live data from Hacker News

Writing HTML with Racket and X-Expressions (2019)

xy2.dev

21–30 of 30 posts

Re: Writing HTML with Racket and X-Expressions (2019)

#21
Something similar (and less of a parentheses hell) is Scalatags by the amazingly prolific scala programmer lihaoyi. The good things about Scalatags is that the same code can be used both in the backend and in the frontend. (I don't use scala on the backend anymore but Scala.js is really good!)

Re: Writing HTML with Racket and X-Expressions (2019)

#22
post #2

The idea is right, but all the 'quoting and explicit list calls make this super awkward. If you're going to do it, do it right... use macros. Of course, then you'd end up something that looks a lot like Haml ( http://haml.info/ ) with parens everywhere.

https://koyoweb.org/haml/index.html uses macros to add haml-inspired features on top of xexprs.

Re: Writing HTML with Racket and X-Expressions (2019)

#23

Something similar (and less of a parentheses hell) is Scalatags by the amazingly prolific scala programmer lihaoyi. The good things about Scalatags is that the same code can be used both in the backend and in the frontend. (I don't use scala on the backend anymore but Scala.js is really good!)

In one of my current projects, I'm using Scalatags on the server-side generating strings, and then using the same templates on the client to generate Preact.js vdom nodes to re-hydrate the server side templates. Cross-platform, cross-backend templates is really a very nice thing to have!

Re: Writing HTML with Racket and X-Expressions (2019)

#25
Not exactly shiny technology, but XQuery seems to have a nice syntax [1]:

  element html {
    element body {
      element div {
        attribute id {"main"},
        "foo bar!",
        1 to 15,
        element footer { "this is the footer" }
      }
    }
  }

  
    
       foo bar! 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15this is the footer
       
    
  
1: https://www.w3.org/TR/xquery-31/#id-computedConstructors

Re: Writing HTML with Racket and X-Expressions (2019)

#27
post #26

Can someone explain the difference between an S-expression and an X-expression? Even after reading the blog post I'm not following, seem like the same thing to me.

Started dabling recently so Im not an expert on the subject, but from my understanding, Racket represents XML data as an X-expression. On the other hand symbolic expressions aka s-expressions are the syntactic elements of the Lisp programming language. Both programs and data are represented as s-expressions: an s-expression may be either an atom or a list. So x-expressions relate to XML

Re: Writing HTML with Racket and X-Expressions (2019)

#29
post #26

Can someone explain the difference between an S-expression and an X-expression? Even after reading the blog post I'm not following, seem like the same thing to me.

X-expressions (as [1]) is simply a subset of S-expressions.

[1] https://docs.racket-lang.org/teachpack/2htdpbatch-io.html?q=...

Re: Writing HTML with Racket and X-Expressions (2019)

#30
post #26

Can someone explain the difference between an S-expression and an X-expression? Even after reading the blog post I'm not following, seem like the same thing to me.

Started dabling recently so Im not an expert on the subject, but from my understanding, Racket represents XML data as an X-expression. On the other hand symbolic expressions aka s-expressions are the syntactic elements of the Lisp programming language. Both programs and data are represented as s-expressions: an s-expression may be either an atom or a list. So x-expressions relate to XML

Generating HTML is a common task. Over the years we have seen multiple approaches in the Racket community. Representing HTML as S-expressions is common. There is even two approaches in use `xexprs` and `sxml`.

There are other solutions, such as representing HTML as structures (often called records in other languages). This is what I prefer - this allows a little static checking. (See `scribble/html` or `urlang/html`).

Others prefer to work with `templates`.

My point got lost: There is more than one "Racket way" of working with html.

Post reply on HN