From: Robert D. <rob...@gm...> - 2014-04-20 18:55:20
|
On 2014-04-20, Leo Butler <l_b...@us...> wrote: > with_gensyms(g,[body]) ::= block( > g:map(lambda([x],funmake("=",[x,gensym(printf(false,"~a_",x))])),g), > psubst(g,buildq([body:body,v:map(second,g)],block(v,splice(body))))); Along these lines, see also blex.lisp which implements a simple-minded lexical block. One would write something like: blex ([t, y], gen_rhs (foo) := ...); Then gen_rhs(foo) returns an expression containing variables named t and y but which are distinct from any other t and y. I think lexical scope is pretty much an absolute requirement for building programs which don't have unpleasant surprises ... maybe someone wants to scope out (hah) a roadmap for bringing lexical scope into Maxima. (Maybe getting rid of dynamic scope too? -- just daydreaming.) best Robert Dodier PS. ;; blex.lisp -- lexical block for Maxima ;; copyright 2012 by Robert Dodier ;; I release this work under terms of the GNU GPL ;; ;; examples: ;; blex ([n:100], f(x) := n : n + x, g() := display(n)); ;; blex ([f], f(x) := 2*x); ;; blex ([a], h(x) := a[x] : 1, i() := arrayinfo(a)); (defmspec $blex (x) (let* ((args (cdr x)) (vars+inits (cdr (car args))) (vars (mapcar #'(lambda (e) (if (symbolp e) e (second e))) vars+inits)) (inits (remove-if #'symbolp vars+inits)) (exprs (cdr args)) (gensym-vars (mapcar #'(lambda (s) (let ((s1 (gensym))) (setf (get s1 'reversealias) (or (get s 'reversealias) s)) s1)) vars)) (subst-eqns (mapcar #'(lambda (x y) `((mequal) ,x ,y)) vars gensym-vars)) (gensym-mprog ($psubstitute `((mlist) ,@ subst-eqns) `((mprog) ((mprogn) ,@ inits) ,@ exprs)))) (meval gensym-mprog))) |