What can you do in LISP that you can't do in Python

Hannah Schroeter hannah at schlund.de
Tue May 22 16:31:13 CEST 2001


Hello!

In article <mailman.989895922.17958.python-list at python.org>,
Steven D. Majewski <sdm7g at Virginia.EDU> wrote:

>[...]

> ( Smalltalk, instead of having these different forms, has a way
>   of slinging around unevaluated code blocks as parameters:

>    object ifTrue: [ block of code to be evaluated ]
>	   ifFalse: [ block of code to be evaluated ] 

This is nothing too much Smalltalk specific, that are just lambdas
in a different light.

Compare:

(defmethod iftrue/false ((boolean (eql nil)) trueCode falseCode)
  (funcall falseCode))

(defmethod iftrue/false
  (boolean #| different from nil is true |# trueCode falseCode)
  (funcall trueCode))

Usage:

(iftrue/false t (lambda () (things) (to-do) (if) (true))
                (lambda () (things) (to-do) (if) (false)))

(iftrue/false nil (lambda () (things) ...)
                  (lambda () (code-for-false)))

>[...]

Smalltalk blocks can also have formal parameters, such as

#(1 2 3) collect: [:a | a + 1]

-> #(2 3 4)

Which is just the same as (map 'vector #'(lambda (a) (+ a 1)) #(1 2 3))
in Lisp, or map (lambda x: x+1, [1,2,3]) in Python.

Or back to Smalltalk's #ifTrue:ifFalse:

class True:
  def ifTrueFalse(self, foo, bar):
    return foo()

class False:
  def ifTrueFalse(self, foo, bar):
    return bar()

t = True()
f = False()

Now,

t.ifTrueFalse(lambda: 42, lambda: 1/0)

gives 42, without giving a divide by zero exception.

Facit: Smalltalk's blocks, Lisp's and Python's lambdas are just the same:
unevaluated (but possibly compiled!) code, optionally with formal
parameters.

Kind regards,

Hannah.



More information about the Python-list mailing list