* Function argument order of evaluation
@ 2019-02-11 9:53 Tadeus Prastowo
2019-02-11 14:54 ` YUE Daian
2019-02-11 14:58 ` tomas
0 siblings, 2 replies; 5+ messages in thread
From: Tadeus Prastowo @ 2019-02-11 9:53 UTC (permalink / raw)
To: help-gnu-emacs
Hello,
In C/C++, the following code has an undefined behavior (UB):
int my_var = 1;
my_function((my_var = 10), 2 * my_var);
It is because their respective standards do not specify that the
assignment `(my_var = 10)' as the first argument must be evaluated
before the second argument `2 * my_var' is evaluated. So,
`my_function' can see as its arguments either `10' and `20' or `10'
and `2'. Compiling the following code with GCC 5.5 that comes with
Ubuntu 16.04 gives the latter:
#include <stdio.h>
void my_function(int a, int b) {
printf("%d, %d\n", a, b);
}
int main() {
int my_var = 1;
my_function((my_var = 10), 2 * my_var);
return 0;
}
Does Emacs Lisp behave the same or does it provide a guarantee that
the function arguments are always evaluated from left to right?
I have searched Emacs Lisp manual and the archive of this mailing list
for the keyword "order of evaluation" but have not found the answer.
So, I ask directly here. Sorry if I might have missed the obvious.
Thank you for your kind help.
--
Best regards,
Tadeus
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Function argument order of evaluation
2019-02-11 9:53 Function argument order of evaluation Tadeus Prastowo
@ 2019-02-11 14:54 ` YUE Daian
2019-02-11 15:15 ` Tadeus Prastowo
2019-02-11 14:58 ` tomas
1 sibling, 1 reply; 5+ messages in thread
From: YUE Daian @ 2019-02-11 14:54 UTC (permalink / raw)
To: help-gnu-emacs
On 2019-02-11 10:53, Tadeus Prastowo <tadeus.prastowo@unitn.it> wrote:
> Hello,
>
> In C/C++, the following code has an undefined behavior (UB):
>
> int my_var = 1;
> my_function((my_var = 10), 2 * my_var);
>
> It is because their respective standards do not specify that the
> assignment `(my_var = 10)' as the first argument must be evaluated
> before the second argument `2 * my_var' is evaluated. So,
> `my_function' can see as its arguments either `10' and `20' or `10'
> and `2'. Compiling the following code with GCC 5.5 that comes with
> Ubuntu 16.04 gives the latter:
>
> #include <stdio.h>
> void my_function(int a, int b) {
> printf("%d, %d\n", a, b);
> }
> int main() {
> int my_var = 1;
> my_function((my_var = 10), 2 * my_var);
> return 0;
> }
>
> Does Emacs Lisp behave the same or does it provide a guarantee that
> the function arguments are always evaluated from left to right?
>
> I have searched Emacs Lisp manual and the archive of this mailing list
> for the keyword "order of evaluation" but have not found the answer.
> So, I ask directly here. Sorry if I might have missed the obvious.
>
> Thank you for your kind help.
>
> --
> Best regards,
> Tadeus
Hi Tadeus,
From the Emacs Lisp reference:
...then the forms in the function body are evaluated in order, and the
value of the last body form becomes the value of the function call.
You see, the forms in the function body are evaluated *in order*.
I think it can be treated as a guarantee? ;-)
Reference:
https://ftp.gnu.org/old-gnu/Manuals/elisp-manual-20-2.5/html_chapter/elisp_9.html#SEC114
Danny
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Function argument order of evaluation
2019-02-11 14:54 ` YUE Daian
@ 2019-02-11 15:15 ` Tadeus Prastowo
0 siblings, 0 replies; 5+ messages in thread
From: Tadeus Prastowo @ 2019-02-11 15:15 UTC (permalink / raw)
To: YUE Daian; +Cc: help-gnu-emacs
On Mon, Feb 11, 2019 at 4:01 PM YUE Daian <sheepduke@gmail.com> wrote:
>
> On 2019-02-11 10:53, Tadeus Prastowo <tadeus.prastowo@unitn.it> wrote:
> > Hello,
> >
> > In C/C++, the following code has an undefined behavior (UB):
> >
> > int my_var = 1;
> > my_function((my_var = 10), 2 * my_var);
> >
> > It is because their respective standards do not specify that the
> > assignment `(my_var = 10)' as the first argument must be evaluated
> > before the second argument `2 * my_var' is evaluated. So,
> > `my_function' can see as its arguments either `10' and `20' or `10'
> > and `2'.
[...]
> Hi Tadeus,
Hi Danny,
> From the Emacs Lisp reference:
> ...then the forms in the function body are evaluated in order, and the
> value of the last body form becomes the value of the function call.
>
> You see, the forms in the function body are evaluated *in order*.
>
> I think it can be treated as a guarantee? ;-)
Thank you for responding. But, my question is not about the
evaluation of the function body but about the invocation of the
function itself.
> Reference:
> https://ftp.gnu.org/old-gnu/Manuals/elisp-manual-20-2.5/html_chapter/elisp_9.html#SEC114
>
> Danny
--
Best regards,
Tadeus
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Function argument order of evaluation
2019-02-11 9:53 Function argument order of evaluation Tadeus Prastowo
2019-02-11 14:54 ` YUE Daian
@ 2019-02-11 14:58 ` tomas
2019-02-11 15:17 ` Tadeus Prastowo
1 sibling, 1 reply; 5+ messages in thread
From: tomas @ 2019-02-11 14:58 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 898 bytes --]
On Mon, Feb 11, 2019 at 10:53:06AM +0100, Tadeus Prastowo wrote:
> Hello,
[about C's funcall arguments evaluation order]
> Does Emacs Lisp behave the same or does it provide a guarantee that
> the function arguments are always evaluated from left to right?
It's left-to-right. From the Elisp manual, "10.2.5 Evaluation of Function
Forms":
"If the first element of a list being evaluated is a Lisp function
object, byte-code object or primitive function object [...] The
first step in evaluating a function call is to evaluate the remaining
elements of the list from left to right."
This seems to be consensus in most of the (traditional) Lisps. Scheme
departed from that, specifying unspecified evaluation order, which
created some stir at the time in comp.lang.scheme. There were (are?)
Schemes which evaluate the arguments left-to-right.
Cheers
-- tomás
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Function argument order of evaluation
2019-02-11 14:58 ` tomas
@ 2019-02-11 15:17 ` Tadeus Prastowo
0 siblings, 0 replies; 5+ messages in thread
From: Tadeus Prastowo @ 2019-02-11 15:17 UTC (permalink / raw)
To: tomas; +Cc: help-gnu-emacs
On Mon, Feb 11, 2019 at 4:03 PM <tomas@tuxteam.de> wrote:
>
> On Mon, Feb 11, 2019 at 10:53:06AM +0100, Tadeus Prastowo wrote:
> > Hello,
>
> [about C's funcall arguments evaluation order]
>
> > Does Emacs Lisp behave the same or does it provide a guarantee that
> > the function arguments are always evaluated from left to right?
>
> It's left-to-right. From the Elisp manual, "10.2.5 Evaluation of Function
> Forms":
>
> "If the first element of a list being evaluated is a Lisp function
> object, byte-code object or primitive function object [...] The
> first step in evaluating a function call is to evaluate the remaining
> elements of the list from left to right."
Yes, this is the answer. I really have missed the obvious statement
there. Thank you very much for your help.
> This seems to be consensus in most of the (traditional) Lisps. Scheme
> departed from that, specifying unspecified evaluation order, which
> created some stir at the time in comp.lang.scheme. There were (are?)
> Schemes which evaluate the arguments left-to-right.
Interesting. Once again, thank you.
> Cheers
> -- tomás
--
Best regards,
Tadeus
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2019-02-11 15:17 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-02-11 9:53 Function argument order of evaluation Tadeus Prastowo
2019-02-11 14:54 ` YUE Daian
2019-02-11 15:15 ` Tadeus Prastowo
2019-02-11 14:58 ` tomas
2019-02-11 15:17 ` Tadeus Prastowo
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).