Hacker News new | ask | show | jobs
by TOGoS 364 days ago
This is fine and interesting, but what I think is lacking in S-expression isn't funky vertical syntax, but a way to directly represent objects that are not lists. Otherwise one needs to invent some representation on top of S-expressions (and then a list isn't necessarily a list anymore; everything goes through an additional layer of encoding/decoding, losing the elegance of S-expressions), or use some extension syntax (usually involving '#'), which varies from language to language and might not even be interpreted by the reader (but logically expand to some list expression that needs to be interpreted again later, so you're not really any better off than with the first approach).

I kind of want something like, to borrow JSON-like syntax and gloss over namespacing issues:

  (foo .
    {type: listy-cons-cell
     head: bar
     tail: (baz quux)})
...which would be another way to say (foo bar baz quuz), but would make it possible to represent any data structure you like at the same level as atoms, strings, and lists.
2 comments

See Clojure’s reader syntax: https://www.clojure.org/reference/reader

You can have vectors, hash maps, and sets in addition to lists, symbols, and keywords.

I don't get why anyone even tries after Clojure. They got it 100% right. It's easier to read than anything else, and still super simple to parse. Commas are whitespace, use them or don't, where ever you want. Namespaced keywords are great. The data structures themselves act as functions. It's just... done.
Yep. There are a couple of places where I think Clojure made the wrong choices, but they are few and far between. Overall, there is so much that was done correctly. I don’t even know if I could program without persistent data structures anymore, for instance.
Kernel has first-class environments which aren't just lists, but can be constructed from lists. Environments are encapsulated, so we can't simply peek into them with car and cdr - we can only obtain the value associated with a given symbol by evaluating the symbol in that environment.

    ($define! foo
        ($bindings->environment
            (bar "Hello World")
            (baz 1234)
            (qux #f)))
            
    ($remote-eval bar foo)          ==> "Hello World"

    foo                             ==> #[environment]
We could perhaps make something a bit more friendly. Lets create an encapsulated `struct` type which could give us the contents as a plain list, or let us look up each field:

    ($provide! ($struct struct? destruct $get)
            
        ($define! (struct-intro struct? struct-elim) 
            (make-encapsulation-type))
                
        ($define! destruct
            ($lambda (struct)
                (cdr (struct-elim struct))))
    
        ($define! $get
            ($vau (struct member) e
                ($let ((record (car (struct-elim (eval struct e)))))
                    (eval member record))))
                    
        ($define! zip
            ($lambda (keys values)
                ($if ($and? (null? keys) (null? values))
                     ()
                     (cons (list (car keys) (car values)) (zip (cdr keys) (cdr values))))))
                    
        ($define! $struct
            ($vau kvpairs env
                ($let* ((keys (map car kvpairs))
                        (values (map ($lambda (pair) (eval (cadr pair) env)) kvpairs))
                        (record (apply (wrap $bindings->environment) (zip keys values))))
                    (struct-intro (cons record values))))))
Example usage:

    ($define! foo
        ($struct
            (bar "Hello World")
            (baz (+ 12 43))
            (qux #f)))              ==> #inert
            
    (struct? foo)                   ==> #t
    (pair? foo)                     ==> #f
    (environment? foo)              ==> #f
    
    (destruct foo)                  ==> ("Hello World" 55 #f)
    
    ($get foo bar)                  ==> "Hello World"
    ($get foo baz)                  ==> 55
    ($get foo qux)                  ==> #f
    ($get foo foo)                  ==> ERROR: Unbound symbol: foo

    foo                             ==> #[encapsulation]
Kernel: https://web.cs.wpi.edu/~jshutt/kernel.html

Klisp (essentially complete implementation of Kernel): https://github.com/dbohdan/klisp