On 4.2.2012 12:14, Antti J Ylikoski wrote:
On 4.2.2012 4:47, Chris Rebert wrote:
On Fri, Feb 3, 2012 at 4:27 PM, Antti J
Ylikoski<antti.yliko...@tkk.fi> wrote:

In Python textbooks that I have read, it is usually not mentioned that
we can very easily program Common LISP-style closures with Python. It
is done as follows:

-------------------------------------

# Make a Common LISP-like closure with Python.
#
# Antti J Ylikoski 02-03-2012.

def f1():
n = 0
def f2():
nonlocal n
n += 1
return n
return f2
<snip>
i. e. we can have several functions with private local states which
are kept between function calls, in other words we can have Common
LISP-like closures.

Out of curiosity, what would be non-Common-Lisp-style closures?

Cheers,
Chris

I understand that a "closure" is something which is typical of
functional programming languages. -- Scheme-style closures, for example.

I don't know Haskell, ML etc. but I do suspect that we could create
closures in those languages as well. Maybe someone more expert than me
can help?

regards, Andy


This is how it is done in standard Common LISP:

-----------------------------------------

;;; Closure with Common LISP.
;;;
;;; Antti J Ylikoski 02-03-2012.

(defun mak-1 ()
  (let ((n 0))
    #'(lambda () (incf n))))

-----------------------------------------

kind regards, Andy

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to