Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
How to achieve side effect in functions
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Eqbal Z  
View profile  
 More options Dec 15 2002, 7:08 pm
Newsgroups: comp.lang.lisp
From: ezaf...@pioneer-usa.com (Eqbal Z)
Date: 15 Dec 2002 16:08:22 -0800
Local: Sun, Dec 15 2002 7:08 pm
Subject: How to achieve side effect in functions
Hi,

I need to write a lisp function rev(lst revlist) which reverses the
list lst and stores it in revlist.
I am able to write the rev(lst) function but how do I write the
function so the result is stored in one of its arguments?

Thanks


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jochen Schmidt  
View profile  
 More options Dec 15 2002, 7:22 pm
Newsgroups: comp.lang.lisp
From: Jochen Schmidt <j...@dataheaven.de>
Date: Mon, 16 Dec 2002 01:19:00 +0100
Subject: Re: How to achieve side effect in functions

Eqbal Z wrote:
> Hi,

> I need to write a lisp function rev(lst revlist) which reverses the
> list lst and stores it in revlist.
> I am able to write the rev(lst) function but how do I write the
> function so the result is stored in one of its arguments?

Lisp has call-by-value. You can simulate what you want by making "rev"
a macro.

Having said that you should definitely look up some information about
returning multiple values in Common Lisp.

ciao,
Jochen

--
http://www.dataheaven.de


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Simon András  
View profile  
 More options Dec 16 2002, 6:59 am
Newsgroups: comp.lang.lisp
From: asi...@math.bme.hu (Simon András)
Date: 16 Dec 2002 13:59:03 +0100
Local: Mon, Dec 16 2002 7:59 am
Subject: Re: How to achieve side effect in functions

ezaf...@pioneer-usa.com (Eqbal Z) writes:
> Hi,

> I need to write a lisp function rev(lst revlist) which reverses the
> list lst and stores it in revlist.
> I am able to write the rev(lst) function but how do I write the
> function so the result is stored in one of its arguments?

If you're willing to pass a cons as the second argument, then you can
(but shouldn't) do this by setf-ing its car and cdr to that of the
reversed list. But note that CL is not C, so only do this if it's a
homework assignment and you're desperate.

Andras


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Naggum  
View profile  
 More options Dec 16 2002, 9:46 am
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.no>
Date: 16 Dec 2002 14:46:27 +0000
Local: Mon, Dec 16 2002 9:46 am
Subject: Re: How to achieve side effect in functions
* ezaf...@pioneer-usa.com (Eqbal Z)
| I need to write a lisp function rev(lst revlist) which reverses the
| list lst and stores it in revlist.

  Why do you think you need this?

--
Erik Naggum, Oslo, Norway

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kaz Kylheku  
View profile  
 More options Dec 16 2002, 2:41 pm
Newsgroups: comp.lang.lisp
From: k...@ashi.footprints.net (Kaz Kylheku)
Date: 16 Dec 2002 11:41:41 -0800
Local: Mon, Dec 16 2002 2:41 pm
Subject: Re: How to achieve side effect in functions

ezaf...@pioneer-usa.com (Eqbal Z) wrote in message <news:3b440a00.0212151608.2d5c8bb6@posting.google.com>...
> Hi,

> I need to write a lisp function rev(lst revlist) which reverses the

You don't have to call list variables LST in Lisp. Even though there
is a function named LIST, you can still use it as a variable name.

> list lst and stores it in revlist.
> I am able to write the rev(lst) function but how do I write the

Lisp already has a REVERSE function, so you wasted your time.

> function so the result is stored in one of its arguments?

Try writing:

   (setf revlist (reverse list))

If you want a special notation for this, write a macro which
translates that notation into the above, e.g.

   (defmacro reverse-into (source-list target-place)
     `(setf ,target-place (reverse ,source-list)))

I can't think of any good reason for wanting this, but there you have
it.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eqbal Z  
View profile  
 More options Dec 16 2002, 3:20 pm
Newsgroups: comp.lang.lisp
From: ezaf...@pioneer-usa.com (Eqbal Z)
Date: 16 Dec 2002 12:20:50 -0800
Local: Mon, Dec 16 2002 3:20 pm
Subject: Re: How to achieve side effect in functions
asi...@math.bme.hu (Simon Andr s) wrote in message <news:vcdbs3mp1bc.fsf@tarski.math.bme.hu>...

> ezaf...@pioneer-usa.com (Eqbal Z) writes:

> > Hi,

> > I need to write a lisp function rev(lst revlist) which reverses the
> > list lst and stores it in revlist.
> > I am able to write the rev(lst) function but how do I write the
> > function so the result is stored in one of its arguments?

> If you're willing to pass a cons as the second argument, then you can
> (but shouldn't) do this by setf-ing its car and cdr to that of the
> reversed list. But note that CL is not C, so only do this if it's a
> homework assignment and you're desperate.

> Andras

Yes this is a homework assignment. How do you pass cons as an argument?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Simon András  
View profile  
 More options Dec 16 2002, 7:46 pm
Newsgroups: comp.lang.lisp
From: asi...@math.bme.hu (Simon András)
Date: 17 Dec 2002 02:46:28 +0100
Local: Mon, Dec 16 2002 8:46 pm
Subject: Re: How to achieve side effect in functions

(rev this-is-your-list (list nil))

(list nil) is a cons. But any cons, that is, anything (call it x) for
which (typep x 'cons) returns T would do.

But see the other replies. Mine was just an attempt to show that you
can do what you want to with a function, depending on how you're going
to use it.

Andras


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Simon András  
View profile  
 More options Dec 17 2002, 12:26 am
Newsgroups: comp.lang.lisp
From: asi...@math.bme.hu (Simon András)
Date: 17 Dec 2002 07:26:01 +0100
Local: Tues, Dec 17 2002 1:26 am
Subject: Re: How to achieve side effect in functions

asi...@math.bme.hu (Simon András) writes:
> > Yes this is a homework assignment. How do you pass cons as an argument?

> (rev this-is-your-list (list nil))

Perhaps I should add that although this answers your quoted question,
considering the wider context, I should've written

(let ((this-will-be-the-reversed-list (list nil)))
  (rev this-is-your-list this-will-be-the-reversed-list)
  ;; here this-will-be-the-reversed-list is already the reversed list
  ;; you do whatever you want with it
  )

Andras


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pascal Bourguignon  
View profile  
 More options Dec 19 2002, 7:19 pm
Newsgroups: comp.lang.lisp
From: Pascal Bourguignon <p...@informatimago.com>
Date: 20 Dec 2002 01:19:48 +0100
Local: Thurs, Dec 19 2002 7:19 pm
Subject: Re: How to achieve side effect in functions

Since you want probably recover the modified value after the call, you
need to  keep a reference to  the cons. So  first, you assign it  to a
variable (theta  in my case) then  you call the  function passing that
variable 'by value'.   Just that a cons contains  two pointers, so you
effectively passed a reference to the function. Note that in my case I
only modify the  cdr of the cons but  both the car and the  cdr can be
used.

(defun fun (n output)
  (do ((i 0 (1+ i)))
      ((> i n))
    (push i (cdr output))))

(show
 (let ((theta (cons nil nil)))
   (fun 3 theta)
   (cdr theta))
 )

==> (3 2 1 0)

Of course,  instead of  having an 'output'  or 'inout'  parameter that
needs such  a special initialization  before calling, you may  want to
have  a input  parameter  and recover  an  output as  result, maybe  a
multiple result if the function must return other values too.

So, instead of writting:  
                          (setq inout (cons nil variable))
                          (setq result (fun n inout))
                          (setq variable (cdr inout))

    and using (setf (cdr inout) ...) and (cdr inout) in the function fun,

     (defun fun (n inout)
        ;; ...
        (setf (cdr inout) (f (cdr inout)))
        ;; ...
        result)

  [ note the equivalence with C:

                         result=fun(n,&variable);

        result_t fun(int n,variable_t* inout)
        {
            /* ... */
            (*inout)=f(*inout);
            /* ... */
            return(result);
        }

  ]

you may also write:

    (multiple-value-bind (res out) (fun n variable)
        (setq result res)
        (setq variable out))

and use (setq inout) and inout  in the function fun and (values result
inout) at the end:

    (defun fun (n inout)
        ;; ...
        (setq inout (f inout))
        ;; ...
        (values result inout))

  [ note the equivalence with C: oops, you can only return one result here...

                         variable=fun(n,variable);

        variable_t fun(int n,variable_t inout)
        {
            /* ... */
            inout=f(inout);
            /* ... */
            return(inout);
        }

  ]

--
__Pascal_Bourguignon__                   http://www.informatimago.com/
----------------------------------------------------------------------
There is a fault in reality. do not adjust your minds. -- Salman Rushdie


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »