[Python-ideas] One more time... lambda function <--- from *** signature def.

Chris Angelico rosuav at gmail.com
Tue Mar 4 16:11:42 CET 2014


On Wed, Mar 5, 2014 at 1:55 AM, Masklinn <masklinn at masklinn.net> wrote:
> I don't agree with this, again why would the thunk be evaluated twice?
> If thunks are added to *delay* expression evaluation (which is what I
> understood from Steven's messages) surely something akin to Haskell's
> semantics is simpler to understand and implement. That is, instead
> of thunks being sugar for:
>
>     bar = lambda: expr
>
> they're sugar for
>
>     bar = memoize(lambda: expr)

Okay. That's an interesting point, and a distinction from lambda.
Effectively, once a thunk is evaluated once, it devolves to its value.
That would be *extremely* interesting in the case of lazy evaluation -
you could actually make a ternary-if function:

value = true_expr if cond else false_expr

def if_(cond, true_thunk, false_thunk):
    if cond: return true_thunk
    return false_thunk

value = if_(cond, `true_expr`, `false_expr`)

There's still the questions of scoping, but now you have the
distinction between a thunk and a function. And if it's at all
possible, the thunk could even replace itself in memory with its
result - that would be massively implementation-dependent, but since
you can't look at the identity of the thunk itself anyway, it wouldn't
break anything. Effectively, as soon as you evaluate bar, it assigns
to bar whatever the expression evaluates to. (Which is simpler and
cleaner to explain than the memoization.)

ChrisA


More information about the Python-ideas mailing list