Re: Needing a trick with 'de
> My question: which mechanism frees X of the value it was setq-ed? During function execution, the variables (symbols actually) used as parameters (in the function definition) are like in an implicit (let): When executing, the values are bound to the arguments as specified in the function call, or NIL if the parameter is omitted in the call. So if the variable (symbol actually) has a value outside of the scope of the function (during function call!), it regains that value when the scope of the function is left, if it has value NIL it regains the value NIL. So it has not to do with the behaviour of (setq) but how function parameters work. I always saw it similar to C, where function parameter variables are like locally declared variables.
Re: Needing a trick with 'de
On Mon, Oct 16, 2017 at 03:44:54PM +0200, Christophe Gragnic wrote: > Because I thought that setq would store a value in the parameter var > "more permanently". > Something like: > > : (foo (need 3)) >(need 3) >-> 3 > : X > -> (need 3) # what I expected > > I thought using a let was necessary to free X afterwards, but even > using setq, X was freed: > > : (foo (need 3)) >(need 3) >-> 3 > : X > -> NIL # what really occured > > My question: which mechanism frees X of the value it was setq-ed? I recommend to read about the concepts of "binding". It is the core of Lisp. ♪♫ Alex -- UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
Re: Needing a trick with 'de
On Mon, Oct 16, 2017 at 3:25 PM, Alexander Burgerwrote: > This is just unnecessary overhead, as 'let' is basically 'bind' (or 'use' to > be > more correct) plus 'setq'. And 'X' is bound already by the function call. So > why > waste time and (stack)space? Because I thought that setq would store a value in the parameter var "more permanently". Something like: : (foo (need 3)) (need 3) -> 3 : X -> (need 3) # what I expected I thought using a let was necessary to free X afterwards, but even using setq, X was freed: : (foo (need 3)) (need 3) -> 3 : X -> NIL # what really occured My question: which mechanism frees X of the value it was setq-ed? chri -- UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
Re: Needing a trick with 'de
Hi Christophe, > I'm quite surprised of the use of setq instead of let. > Could you please elaborate? Do you mean, staying with the generated example code: (de foo X (setq X (car X)) (msg X) (length (eval X)) ) to generate instead: (de foo X (let X (car X) (msg X) (length (eval X)) ) ) This is just unnecessary overhead, as 'let' is basically 'bind' (or 'use' to be more correct) plus 'setq'. And 'X' is bound already by the function call. So why waste time and (stack)space? ♪♫ Alex -- UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
Re: Needing a trick with 'de
On Sat, Oct 14, 2017 at 7:48 AM, Alexander Burgerwrote: > You are close :) Hi Alex, Thanks, I'm making some progress at last! A personal lesson: when meta-programming, I should make more use of pp. > I would do this: > >(de decar Lst > (def (++ Lst) > (let Var (++ Lst) > (cons >Var >(list 'setq Var (list 'car Var)) >Lst ) ) ) ) Thanks, it works at home too, but with pop instead of ++ (I'm using EmuLisp). I just replaced (++ Lst) with (pop 'Lst). I'm quite surprised of the use of setq instead of let. Could you please elaborate? More precisely, I would have thought that (foo smthng) would have bound 'smthng to X. chri -- UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
Re: Needing a trick with 'de
On Sat, 14 Oct 2017 07:48 +0200, Alexander Burger wrote: > Test: > > [...] > >: (foo (need 3)) >(need 3) >-> 3 Rounding off the test: : (foo (need 3) 'this-and-subsequent-will-be-ignored 42 (/ 1 0)) (need 3) -> 3 Of course. :) -- UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
Re: Needing a trick with 'de
Hi Christophe, > (decar myfn arg > # here arg is the first arg given when calling myfn, unevaluated > ) > > > This is my attempt: > > (de decar args > (let (fn (car args) > arg (cadr args) > body (cddr args)) > (set fn (list (list "arg") (list 'let arg "arg" (list 'run body)) You are close :) I would do this: (de decar Lst (def (++ Lst) (let Var (++ Lst) (cons Var (list 'setq Var (list 'car Var)) Lst ) ) ) ) Test: : (decar foo X (msg X) (length (eval X)) ) -> foo : (pp 'foo) (de foo X (setq X (car X)) (msg X) (length (eval X)) ) -> foo : (foo (need 3)) (need 3) -> 3 ♪♫ Alex -- UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe