4 ms·
Have they ever fixed the problem with let not being implemented (which was the case when I looked at it a couple of years ago). Lisp-like languages without let
by jhbadger 5y ago
Have they ever fixed the problem with let not being implemented (which was the case when I looked at it a couple of years ago). Lisp-like languages without let lose a lot of the inherent power of lisps.
- rcarmo 5y agoIt's sort of back, implemented as a macro. I've yet to go back to Hy (I left because of the removal of let, plus other things I found missing), but I try it every now and then and the last time I did the namespacing was still a tad weird.
- gilch 5y agoHave you tried Hissp yet?
- EnderShadow8 5y agoThis might be interesting https://pypi.org/project/scoping/ https://pypi.org/project/scoping/
- Kodiologist 5y agoIt's been implemented, found to be subtly broken, and re-implemented a number of times. Ultimately trying to impose a different model of scoping on top of Python's scoping is living in a state of sin; thus the difficulties. The status quo is an apparently working `let` due to a truly heroic effort by Sunjay Cauligi ( https://github.com/hylang/hy/pull/2125 https://github.com/hylang/hy/pull/2125 ). You're encouraged to try to find bugs in it. In truth, I've never understood the fuss about `let`. It's taken a huge amount of Hy development effort, but it adds very little. Python's native scoping works well in the large majority of cases, and in tricky cases, I'd rather use `nonlocal` or `global` explicitly so I'm sure what's happening at the level of Python semantics. I suspect that people who heard that previous versions of Hy were missing `let` assumed that some other feature (such as nested lexical scopes) was also missing that has actually always been there.
- contravariant 5y agoWhat problems did you encoutner implementing 'let'? Shouldn't it be equivalent to lambda expressions somehow?
- Kodiologist 5y agoIn some other Lisps, yes, but Python has its own idea of how function scoping works that generally isn't what you'd want with `let`. For example, in (let [a 1] (defn foo [] (setv a 2)) (foo) (print a)) you probably `2` to be printed, but the seeming equivalent Python code a = 1 def foo(): a = 2 foo() print(a) prints `1`. Python interprets the assignment as declaring a new local variable. Frankly, Python got scoping wrong (in its quest to eliminate declarations), but here we are. So `let` needs to work around this somehow. The current implementation implicitly adds `global a` or `nonlocal a` as appropriate. (Actually, the name `a` is also mangled so that any references to `a` outside the `let` refer to a different variable.)
- ashtonbaker 5y agothis made me wonder - I know I can use global variables inside a function definition: a = 1 def foo(): x = a foo() so if this 'a' is a new variable, what happens when the name collides with a global variable? a = 1 def foo(): a = a foo() > UnboundLocalError: local variable 'a' referenced before assignment yuck!
- all2 5y agoI'm not sure this is so much of a problem? Changing what you name the variable fixes the issue. Python is generally quite good at having a verbose option to express things, so I'm surprised there's not an explicit way to reference the enclosing scope from an object.
- vegai_ 5y agoIs there any non-horror piece of code where supporting this latter case would be useful?
- mark_l_watson 5y agoThey are working on improving it. I wrote a short little book on Hy and I initially used the “let” macro. I stopped using it because in general Hy code can be automatically translated to very readable Python code, but using the “let” macro generates ugly Python code.