5 ms·
defn nested in defn seems totally wrong from a LISP (or Clojure) viewpoint. Isn't there a let or letfn? (Please note that I have no idea of Python)
by the-kenny 13y ago
defn nested in defn seems totally wrong from a LISP (or Clojure) viewpoint. Isn't there a let or letfn? (Please note that I have no idea of Python)
- deleted 13y ago[deleted]
- thaumasiotes 13y agoin Python one function definition inside another is totally normal, and (to my knowledge) the statement doesn't generate a global binding, so that the inner-defined function is not accessible from outside the outer one. In other words, python's def appears to be syntactic sugar for variable assignment within whatever scope you're already in, except of course that there is no nonsugared form. Which would make it analogous to let, I believe.
- babarock 13y agoTechnically, in Python nothing prevents you from writing something like: foo = lambda x: x*2 If only that lambda statements are limited (by design?) to only a single return expression. As far as Hy goes, lambdas won't have this limitation.
- icebraining 13y agoIn a way, there is: >>> mycode = "a = int(raw_input()); print a * 2; return a" >>> myfunc = FunctionType(compile(mycode, "<string>", "exec"), globals()) >>> myfunc() 4 8 4
- wging 13y agoThis SyntaxErrors for me. >>> from types import * >>> mycode = "a = int(raw_input()); print a * 2; return a" >>> myfunc = FunctionType(compile(mycode,"<string>","exec"), globals()) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1 SyntaxError: 'return' outside function Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1 SyntaxError: 'return' outside function
- hga 13y agoIt's not from a Scheme viewpoint, and specifically SICP, which is where he's starting from. Frankly, I'd prefer if it were idiomatic to locally defn inside Clojure defns, letfn is not hardly so elegant or clear. One of a number of areas where I find Clojure needlessly less tasteful and elegant than Scheme, but all are at least tolerable.