Returning by reference

Started by kks, December 25, 2009, 04:36:10 PM

Previous topic - Next topic

kks

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

kks

#1
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"!

cormullion

#2
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...

itistoday

#3
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 http://newlispfanclub.alh.net/forum/viewtopic.php?f=16&t=3413">gen-wrapper function.
Get your Objective newLISP groove on.