unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: richardeng <richardeng@foxmail.com>
To: emacs-devel@gnu.org
Cc: Alan Mackenzie <acm@muc.de>,
	Thierry Volpiatto <thierry.volpiatto@gmail.com>
Subject: Re: Elisp info request: function parameter passing
Date: Wed, 10 Dec 2008 02:50:29 +0800	[thread overview]
Message-ID: <493EBDF5.3030109@foxmail.com> (raw)
In-Reply-To: <493EB229.20308@foxmail.com>

richardeng wrote:
> I just found elisp is different in function parameter passing than C, or 
> we can say elisp is reference passing style. That is, the variable in 
> function call is itself, the same one in elisp language. Following code 
> can show this different feature.
> 
> (setq x '(1 2 3 "one" "two" "three"))
> (defun fun (arg)
>   (if (eq arg x)
>       (message "they are eq, the same one")
>     (message "they are different")))
> (fun x)
> 
> I think we need to add more info about it in (elisp)Top > Functions
> 
> Another question:
> I think change the element of list should be a common request. Why 
> doesn't elisp provide a primitive/function for this feature. Did I miss it?
> 
> ------------------------------------------------------------------------
> 
> Subject:
> Re: How could I modify list element?
> From:
> richardeng <richardeng@foxmail.com>
> Date:
> Wed, 10 Dec 2008 01:43:23 +0800
> To:
> Alan Mackenzie <acm@muc.de>
> 
> To:
> Alan Mackenzie <acm@muc.de>
> CC:
> help-gnu-emacs@gnu.org
> 
> 
> Alan Mackenzie wrote:
>>
>>
>> On Tue, 9 Dec 2008, richardeng wrote:
>>
>>> Hi all,
>>>    setcar/setcdr is not convenient.
>>>    In a long list, ex.
>>>    (setq a '(a b c d e f g))
>>>    I want to change 'e to 'E.
>>>    I need a function: (set-list-elt list old-elt new-elt)
>>>
>>>    How? translate list to vector, modify, then turn it back???
>>
>> Try this (not tested):
>>
>> (defun change-nth (liszt, n, nieuw)
>>   (while (> n 0)
>>     (setq liszt (cdr liszt)
>>           n (1- n)))
>>   (if liszt
>>      (setcar liszt nieuw)))
>>
> (setq aaa '(1 2 3 4 5 b a "ccc"))
> (change-nth aaa 3 'BB)
> aaa  --> (1 2 3 BB 5 b a "ccc")    , it works, thank you!
> 

Additional comment for C programmer:

In C, when you change the value of variable you are changing the 
variable's value, the old value is lost.

In elisp, when you change the value of variable you are changing the 
target of the variable, the old value remains. Old value will be GC 
until zero reference

So, when you pass a variable to a function. The parameter is a new 
variable but point to the same value. If you change the parameter in 
function, you just change the target of the parameter variable. Unless, 
you use setcar/setcdr/etc. to change the value cons. Note, many other 
functions will copy the value.

Lisp is really beautiful and special in design.






  reply	other threads:[~2008-12-09 18:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-12-09 18:00 Elisp info request: function parameter passing richardeng
2008-12-09 18:50 ` richardeng [this message]
2008-12-09 19:44 ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=493EBDF5.3030109@foxmail.com \
    --to=richardeng@foxmail.com \
    --cc=acm@muc.de \
    --cc=emacs-devel@gnu.org \
    --cc=thierry.volpiatto@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).