Fw: Passing primitive value by reference
----- Original Message -----
From: "lisptracker" <lisptracker@mail.ru>
To: "Mark Nahabedian" <naha@MIT.EDU>
Sent: Tuesday, October 05, 2004 5:15 AM
Subject: Re: Passing primitive value by reference
> ----- Original Message -----
> From: "Mark Nahabedian" <naha@MIT.EDU>
> To: "lisptracker" <lisptracker@mail.ru> <lisp-hug@xanalys.com>
> Sent: Tuesday, October 05, 2004 12:57 AM
> Subject: Re: Passing primitive value by reference
>
>
> > On Mon, Oct 04, 2004, 13:20, lisptracker <lisptracker@mail.ru> wrote
> > >Hi
> > >
> > >One my frind, who likes C++ much, asked me How to pass by reference
value
> > of primitive type to function. It's common technique in C/C++ to share
one
> > memory cell between multiple functions passing address of cell as value.
> One
> > way similar to it is to pass value wraped into cons cell. Another way is
> to
> > use dynamic extent or macro, but it is very ugly.
> > >
> > >As far as I know a primitive value being passed to a function, remains
> > unmuttable object. So I have to use some wrapper...
> > >To answer a question I should find some way to do it like below:
> > >
> > >(let ((mutable-var 2))
> > > (function-to-change-var mutable-var)
> > >mutable-var)
> > >
> > >So mutable-var is not equals 2 after calling of function-to-change-var.
> >
> > Why would you want to do this in Lisp? WHat are you really trying to
do?
> > I ask because though C and C++ programers frequently need to pass "by
> > reference" parameters, I as a Lisp programmer don't recall ever having
to
> do
> > so.
>
> He want pass reference to some "place" to many functions which can modify
> their arguments,
> so they can modify that place. He said it is way of sharing mutable value
> between multiple functions
> which were defined somewhere. I can do it with non primitive types easily,
> but for example numbers are immutable.
> Maybe best way is to wrap number to cons cell.
>
> He would thought that is impossible in Lisp. For some reasons I must prove
> him that thing is possible somehow and
> show most elegant way.
>
>
> > The typical use for by-reference parameters in C and C++ is for a
function
> > to return more than one value, e.g. returning a sucess/failure code as
the
> > value of the function and filling in some structure which was passed by
> > reference. The way one would do this in Lisp is to have the function
> return
> > multiple values. In Lisp, functions can return more than one value. In
> > C and C++ they can't.
>
> I know. But looks like ability to return multiple values is not only one
> reason.
>
> For example at first we have closure:
>
> (let ((var 5))
>
> (defun change-var-one ()
> (setf var 6))
>
> (defun change-var-two ()
> (setf var (+ var 7)))
>
> (defun change-var-three ()
> (incf var)))
>
> Now we want to declare change-var-one, change-var-two, change-var-three in
> one place, declare var
> in another place and call these functions passing reference to place:
>
> (let ((place-one 5)
> (place-two 5))
>
> (change-var-one place-one)
> (change-var-one place-two)
> (change-var-two place-one)
> (change-var-two place-two))
>
> More than that, he want to pass reference from outer world.
>
> (defun one-big-fun (reference-place)
> (change-var-one reference-place)
> (change-var-two reference-place)
> )
>
> > If you really need to be able to have some function modify a value that
it
> > doesn;t have access to, you can always pass a closure which modifies the
> > value, e.g.:
> >
> > for some code fragment
> >
> > ...
> > (let ((change-this-value 7))
> > (some-function #'(lambda (new-value)
> > (setq change-this-value new-value)))
> > ;; At this point the value of CHANGE-THIS-VALUE is 4
> > )
> > ...
> >
> > which calls SOME-FUNCTION and expects it to modify the value of
> > CHANGE-THIN-VALUE
> >
> > (defun some-function (value-changer)
> > (funcall value-changer 4))
>
> Anyway, thank you, for your kind advice.
>
> Though it is another way to create a wrapper. Would it be efficient to
call
> function every time to change value ?
>
> Maybe there are some ways to do same thing with totaly different approach
> ?????
>
> So I repeat conditions and question:
> 1) There is some place where primitive value is stored
> 2) There are some functions which should modify places we point on, so
> we can share places between functions.
>
> How to share places between some functions and have ability to point place
> by name for that functions in most
> elegant way (is it closures, or macro, or dynamic vars ...) ?
>
> Best regards
> Lisper
>
>