3 ms·
IIRC scheme and probably common lisp have something like this: (call-with-dynamic-bind ((var1 val1)(var2 val2)...) stuff...) It lets you introduce dynamical
by ruste 9y ago
IIRC scheme and probably common lisp have something like this:
(call-with-dynamic-bind ((var1 val1)(var2 val2)...)
stuff...)
It lets you introduce dynamically scoped variables that work pretty much the way you described.
- kazinator 9y agoIn Common Lisp this is just let, where the variables are dynamic variables. (There is a dynamic binding operator called progv, but the situation for that is when the variable names are computed.)
- ruste 9y agoI don't think so. _let_ is lexically scoped. What I was remembering was a syntax called parameterize in chicken scheme that would bind a set of values to a set of dynamic variables. You can see an example of it here: https://github.com/caolan/snowy/blob/master/server.scm#L85 https://github.com/caolan/snowy/blob/master/server.scm#L85 I don't think common lisp has an equivalent (and I'm pretty sure it's not part of rnrs in scheme), but it should be really easy to hack one together.
- kazinator 9y agoIn Common Lisp let is either lexically or dynamically scoped, depending on whether the symbol is marked "special" which is the situation if the symbol was previously used as the name in a defvar or defparameter form to define a dynamic variable. So dynamically scoped contexts are achieved just by binding these special variables. By convention, they are usually given names beginning and ending with an asterisk ("earmuffs") to put them into an effectively separate namespace. That parametrize is reminiscent of fluid-let; another Scheme hack to simulate dynamic scope.
- ruste 9y agoAaahhh! Yeah, that makes total sense. I was thinking about newly introduced symbols using let. I recently jumped from scheme to common lisp so I'm used to lets just shadowing other variables. Most of the major scheme implementations I've used have had real dynamic scoping of some sort. I don't think it's very well supported in the standard, but they all seem to have it as a proper language feature.
- kazinator 9y agoThe convenient thing about special variables just using let is that you can use them in any binding construct: (with-output-to-string (*standard-output*) ... code ... ) Here, code's output to the default standard output stream is captured in a string.