[Haskell-cafe] Python?

Donn Cave donn at drizzle.com
Wed May 11 12:28:42 EDT 2005

On Wed, 11 May 2005, Jerzy Karczmarczuk wrote:
> Pierre Barbier de Reuille wrote about Python and lambdas:
>> Well, I would not recommand using lambda functions ! The main reason 
>> is they are limited in that they only accept expressions (ie. not 
>> statements) and you can end up with very ugly things (mainly because 
>> of the lack of if-expressions).
> Oh! You would *like to have imperative statements within LAMBDA*?
> Friend, you are in state of mortal sin!
> I suppose that if somebody decides to use lambdas, he wants to do some
> functional programming, no?


> True, there are no conditional expressions. But there are because of the
> laziness of boolean combinators. This works:
> (x > y) or 'allez en enfer'

Yes, and hallelujah, there are other options too -- suppose we
define a helper function (because "exec" is also a statement)
to execute a compiled code object:

def ex(st, env):
        exec(compile(st, '<string>', 'single'), env)

Now we can use this function to compile and execute anything we
like from inside the lambda!

	five = lambda x: ex('if x == 5:\n\tprint "five"\nelse:\n\tprint "not five"', locals())


More information about the Haskell-Cafe mailing list