4 ms·
> I am unaware of any other programming language that has this feature S7 scheme: > (define f (let ((x 5)) (lambda (y) (+ x y)))) f > (f 4) 9 > (obj
by moonchild 5y ago
> I am unaware of any other programming language that has this feature
S7 scheme:
> (define f (let ((x 5)) (lambda (y) (+ x y))))
f
> (f 4)
9
> (object->string f :readable)
"(let ((x 5)) (lambda (y) (+ x y)))"
There is even talk of printing continuations readably.
- bjoli 5y agoIIRC the python-on-guile project can probably do this using some C voodoo. I believe it can serialize continuations and make deep copies of generators with mutable state. Edit: however, I doubt the serialized format is human readable by default...
- taeric 5y agoThis isn't really the same, though. You are printing f. Print the closure returned by f.
- moonchild 5y agoThis is scheme. f doesn't return a closure; it is a closure, and it returns a number.
- taeric 5y agoStill feels different. For example, this (let ((x 3)) (let ((y (lambda (z) (lambda () (+ x z))))) (message "%s" (funcall y 3)) (message "hello") (setq x 5) (message "%s" (funcall y 3)))) will produce the following messages (closure ((z . 3) (x . 3) t) nil (+ x z)) hello (closure ((z . 3) (x . 5) t) nil (+ x z)) While this: (let ((x 3)) (let ((y (lambda (z) (lambda () (let ((w 0)) (+ w x z)))))) (message "%s" (funcall y 3)) (message "hello") (setq x 5) (message "%s" (funcall y 3)))) will print: (closure ((z . 3) (x . 3) t) nil (let ((w 0)) (+ w x z))) hello (closure ((z . 3) (x . 5) t) nil (let ((w 0)) (+ w x z))) Is it just that emacs is more explicit about which values were closed over? Edit: Actually, I see my confusion, you accidentally printed 'f', not 'g', which I think was your intent?
- moonchild 5y ago> Edit: Actually, I see my confusion, you accidentally printed 'f', not 'g', which I think was your intent? I am not quite sure what you mean? There was no 'g' at all in my code snippet except for that in 'object->string'. Unless you are mistaking the '9' for one?
- taeric 5y agoIs what happens when I write so late at night. I mistook the 9 for a g, thinking it was showing the body of the lambda. Really, I was just reading it all sorts of wrong. I still feel this is somehow different, but I can't express the difference well. Note that I should say I am not at all surprised that scheme could do this, for what its worth. I'm just saying that this feels different.
- moonchild 5y agoHere are your snippets in s7, and their output. (let ((x 3)) (let ((y (lambda (z) (lambda () (+ x z))))) (format #t "~a~%" (object->string (y 3) :readable)) (format #t "hello~%") (set! x 5) (format #t "~a~%" (object->string (y 3) :readable)))) Which gives: (let ((z 3) (x 3)) (lambda () (+ x z))) hello (let ((z 3) (x 5)) (lambda () (+ x z))) And: (let ((x 3)) (let ((y (lambda (z) (lambda () (let ((w 0)) (+ w x z)))))) (format #t "~a~%" (object->string (y 3) :readable)) (format #t "hello~%") (set! x 5) (format #t "~a~%" (object->string (y 3) :readable)))) Which gives: (let ((z 3) (x 3)) (lambda () (let ((w 0)) (+ w x z)))) hello (let ((z 3) (x 5)) (lambda () (let ((w 0)) (+ w x z))))
- taeric 5y agoCool, so it is just more explicit it elisp. Curious if there is a reason for that. And thanks for running these! I can't really give a reason for not installing a scheme to try myself... :(
- wdkrnls 5y agoR can do this too if I understand correctly.