[Python-ideas] Symbolic expressions (or: partials and closures from the inside out)

Devin Jeanpierre jeanpierreda at gmail.com
Fri Jan 13 17:24:31 CET 2012


On Fri, Jan 13, 2012 at 9:53 AM, Eike Welk <eike.welk.lists1 at gmx.de> wrote:
> IMHO this is quoting like in Lisp: He stores a bit of code in an un-evaluated
> form. The bit of code is then interpreted somewhere else, in special contexts,
> that he can choose with his hacked version of "isinstance".
>

I wanted to think he wanted quote, because quote is awesome, but that
doesn't gel with his desired behavior inside functions.

def foo(A):
    return (A + 2) * 5

(define (foo A)
    (* (+ A 2) 5)))

If in Python one calls foo(SymbolicExpr + 3), then it returns a
SymbolicExpr that can be lazily evaluated to compute the expression.
This is fairly general and could work even with mutating statements
and all sorts of nasty things.

If, in Scheme, one calls (foo '(+ X 3)), it is an error. There's no
automatic laziness associated with quoted expressions -- they're just
data. If one called (foo '3) then it would outright eagerly evaluate
the function, returning 25, because '3 == 3.

OTOH, you can define a foo that operates on quoted expressions and
would return '(* (+ (+ X 3) 2) 5), as follows:

(define (foo A)
    `(* (+ ,A 2) 5))

That might be sufficient for his use-case, can't tell. It isn't
specifically what he asked for, though. (See also: XY problem).

-- Devin



More information about the Python-ideas mailing list