3 ms·
My lisp is a bit rusty, but it looks like what you're doing there is returning a function which gets redefined every time you reuse the function. The equivalen
by anthonyb 14y ago
My lisp is a bit rusty, but it looks like what you're doing there is returning a function which gets redefined every time you reuse the function.
The equivalent Python would be something like this:
def function():
x = 3
def internal(x, foo=[]):
foo.append([7])
foo.append(x)
return foo
return internal(x)
print function()
print function()
Which does what you would expect:
[[7], 3]
[[7], 3]
- ScottBurson 14y agoNo. In my example the function is created only once, and called twice.
- anthonyb 14y agoWhat's that lambda thingo in the middle then? Pretty sure that's another function, redefined every time your function is called.
- Tyr42 14y agoYes, the lambda creates the function. Note that defvar does not. So there is still only one function being defined here.
- anthonyb 14y agoOk, I see now. You've still got that &optional argument though. I don't see a huge amount of difference from a semantic point of view between that and the Python version though (ie. if x == None: ...).
- derleth 14y agoNo, the lambda expression creates the function, which is returned as the value of the let block; defvar just binds the function to a name so we can use it multiple times.