An accidental discovery: procedures in Emacs-lisp are transparent. (eval '(let ((x 1))
(lambda () (+ x 1))) t)
;; (closure ((x . 1) t) nil (+ x 1)) Although this (closure ...) is a list, it can be invoked directly. ((closure ((x . 1) t) nil (+ x 1)))
;; 2 IMO, it looks more consistent than Racket. Since Racket has transparent struct, why no transparent procedure? It would be nice if we could have both transparent and opaque procedure, just like struct!
Further investigation, about sharing: (setq print-circle t)
(eval '(letrec ((f (lambda (x) (if (> x 0) 0 1))))
f) t)
;; #1=(closure ((f . #1#) t) (x) (if (> x 0) 0 1)) As you can see, when using (setq print-circle t), emacs prints the cyclic structure. Unfortunately, it can not be read by emacs directly, because emacs-lisp does not support <https://docs.racket-lang.org/reference/reader.html#(part._parse-graph)|Reading Graph Structure> like Racket.
At minimum each closure would need to have an extra slot containing information on the variable names. Allocating a transparent closure would therefore take more time. This explains why transparent closures aren’t the default in many languages.
In Racket you can use
https://docs.racket-lang.org/web-server-internal/closure.html
to experiment with transparent (aka serializable) closures.
Also, if you are interested in how closures are represented:
https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.585.6551&rep=rep1&type=pdf
The last paper shows in section 2.2 that a closure might not contain all free variables.
@soegaard2 Thanks. I just tried define-closure, it’s nice, but the serialized form are not readable as the emacs one. > Allocating a transparent closure would therefore take more time. Is it possible to maintain an additional transparent representation just for reification? In general, we do not allocate a lot of closures continuously.
I think, it would be relatively simple to change the printing.
I have chosen to use React with hooks. I like the functional feel of it. I am not using jsx though. I wanted to generate the final JavaScript directly from Urlang without any external tools, so I use a macro that generates the same output as jsx does (but with a different input syntax).
Apropos, React with hooks. There is a new set of documentation: https://beta.reactjs.org/learn
Would love to see that macro :heart_eyes:
It’s fairly short:
https://github.com/soegaard/urlang/blob/master/urlang/react/urx.rkt
I can wrote stuff like: (define (ExerciseAnswerInput props)
(def (on-change e) (props.set-user-answer e.target.value))
@urx[@Form[@InputGroup[
@InputGroup.Prepend[
@InputGroup.Text[@ur[(result-type->text props.result-type)]]]
@Form.Control[placeholder: "Svar"
onChange: on-change
onKeyUp: props.onKeyUp
onKeyDown: props.onKeyDown
type: "text"
ref: props.input-ref
disabled: props.disabled
tabIndex: 0]]
@Form.Text[className: "text-muted"
@ur[props.error-message]]]])
That’s awesome. Not sure why anyone would write JS by hand anymore :slightly_smiling_face:
(define 1+1 (+ 1 1)) :stuck_out_tongue:
I can hear Fry’s voice narrating the very start of Futurama as I watch :smile:
@soegaard2 But this gives me the feeling that it is not “built-in”, because it needs some intermediate structure to do the conversion. Ideally, the printed result can be used (or invoked) directly in a program, like (closure ...) in emacs-lisp.
For example, (struct my-struct (x y) #:transparent)
(my-struct 1 2)
;; => (my-struct 1 2) The printed result (my-struct 1 2) could be used in a Racket program directly. It expresses some homoiconicity of lisp.
Ideally, serialization and deserialization should not appear in lisp, unless performance or space consumption was concerned.