* Elisp info request: function parameter passing
@ 2008-12-09 18:00 richardeng
2008-12-09 18:50 ` richardeng
2008-12-09 19:44 ` Stefan Monnier
0 siblings, 2 replies; 3+ messages in thread
From: richardeng @ 2008-12-09 18:00 UTC (permalink / raw)
To: emacs-devel; +Cc: Alan Mackenzie, Thierry Volpiatto
[-- Attachment #1: Type: text/plain, Size: 648 bytes --]
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?
[-- Attachment #2: Re: How could I modify list element?.eml --]
[-- Type: message/rfc822, Size: 1184 bytes --]
From: richardeng <richardeng@foxmail.com>
To: Alan Mackenzie <acm@muc.de>
Cc: help-gnu-emacs@gnu.org
Subject: Re: How could I modify list element?
Date: Wed, 10 Dec 2008 01:43:23 +0800
Message-ID: <493EAE3B.8020707@foxmail.com>
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!
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Elisp info request: function parameter passing
2008-12-09 18:00 Elisp info request: function parameter passing richardeng
@ 2008-12-09 18:50 ` richardeng
2008-12-09 19:44 ` Stefan Monnier
1 sibling, 0 replies; 3+ messages in thread
From: richardeng @ 2008-12-09 18:50 UTC (permalink / raw)
To: emacs-devel; +Cc: Alan Mackenzie, Thierry Volpiatto
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.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Elisp info request: function parameter passing
2008-12-09 18:00 Elisp info request: function parameter passing richardeng
2008-12-09 18:50 ` richardeng
@ 2008-12-09 19:44 ` Stefan Monnier
1 sibling, 0 replies; 3+ messages in thread
From: Stefan Monnier @ 2008-12-09 19:44 UTC (permalink / raw)
To: richardeng; +Cc: Alan Mackenzie, Thierry Volpiatto, emacs-devel
> 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
You are confused.
> (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)
Try the equivalent C code:
char *x = "hello";
fun (char *arg)
{
if (arg == x)
printf ("they are eq\n");
else
printf ("they are different\n");
}
main ()
{
fun (x);
}
-- Stefan
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2008-12-09 19:44 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-12-09 18:00 Elisp info request: function parameter passing richardeng
2008-12-09 18:50 ` richardeng
2008-12-09 19:44 ` Stefan Monnier
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).