newLISP Fan Club

Forum => newLISP in the real world => Topic started by: kks on December 25, 2009, 04:36:10 PM

Title: Returning by reference
Post by: kks on December 25, 2009, 04:36:10 PM
Hi,



How to "return by reference" from a user-defined function/macro?



(let (l '(a b c)) (pop l) l)   # => (b c)
(let (l '(a b c)) (pop (or l)) l)   # => (b c)
(let (l '(a b c)) (pop (println l)) l)   # => (a b c); is this a bug?
(let (l '(a b c)) (pop (begin (println l) l)) l)   # => (b c)
(let (l '(a b c)) (pop (let () l)) l)   # => (a b c); is this a bug?
(let (l '(a b c)) (pop ((fn () l))) l)   # => (a b c); how to make it return (b c)?


My intention is to write a debugging macro:



(define-macro (%p) (println "% " (args 0) " = " (eval (args 0))))

(let (l '(a b c)) (pop (%p l)) l)   # => (a b c); how to make it return (b c)?


Thanks,

KS
Title: Re: Returning by reference
Post by: kks on December 26, 2009, 04:49:41 AM
By the way, in the following code:

(setq x ((fn () '(a b c))))   # => (a b c)

does it perform ONE (either when returning or binding) or TWO (both) copy operations?



Is it possible to make all sequences return "by reference"? I think this still conforms to ORO (One Reference Only) memory management, as it is "binding" (set, let, parameter binding), not "returning", that causes "referencing"!
Title: Re: Returning by reference
Post by: cormullion on December 26, 2009, 08:20:18 AM
Quote from: "kks"
(let (l '(a b c)) (pop (println l)) l)   # => (a b c); is this a bug?


Hi! I would guess not a bug:


> (set 'l '(a b c))
(a b c)
> (println l)
(a b c)
(a b c)
> (pop (println l))
(a b c)
a
> l
(a b c)


since pop operated on the result returned by println rather than on l. That's only my guess, though...
Title: Re: Returning by reference
Post by: itistoday on December 26, 2009, 08:42:09 AM
User defined functions always pass things around by value, but there are ways of getting around this by passing "containers" by value (i.e. symbols or contexts).



You may want to look at the thread on the gen-wrapper function (//http).