Hacker News new | ask | show | jobs
by the-kenny 4740 days 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)
2 comments

It'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.

in 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.
Technically, 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.

I totally write code like this all the time, when I need a quick one-liner that I don't really care about. A little more then a technicality.
Totally. Hy will even auto-expand a lambda to a function if it's not sane Pythonically :)
In 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
This 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