Dear R developers, sessionInfo() below Please have a look at the following two versions of the same function: 1. Intended behavior:> Su1 = function(u=100, l=u, mu=0.53, sigma2=4.3^2)+ { + print(c(u, l, mu)) # here, l is set to u?s value + u = u/sqrt(sigma2) + l = l/sqrt(sigma2) + mu = mu/sqrt(sigma2) + print(c(u, l, mu)) + }> > Su1()[1] 100.00 100.00 0.53 [1] 23.2558140 23.2558140 0.1232558 In the first version, both u and l are correctly divided by 4.3. 2. Strange behavior:> Su2 = function(u=100, l=u, mu=0.53, sigma2=4.3^2)+ { + # print(c(u, l, mu)) + u = u/sqrt(sigma2) + l = l/sqrt(sigma2) # here, l is set to u?s value + mu = mu/sqrt(sigma2) + print(c(u, l, mu)) + }> > Su2()[1] 23.2558140 5.4083288 0.1232558 In the second version, the print function is commented out, so the variable u is copied to l (lowercase L) at a later place, and L is divided twice by 4.3. Is this behavior intended? It seems strange that the result depends on a debugging message. Best wishes, Matthias> sessionInfo()R version 3.4.1 (2017-06-30) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows >= 8 x64 (build 9200) Matrix products: default locale: [1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 LC_MONETARY=German_Germany.1252 [4] LC_NUMERIC=C LC_TIME=German_Germany.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] compiler_3.4.1 tools_3.4.1 [[alternative HTML version deleted]]
ruipbarradas at sapo.pt
2017-Sep-02 17:33 UTC
[R] Strange lazy evaluation of default arguments
Hello, One way of preventing that is to use ?force. Just put force(l) right after the commented out print and before you change 'u'. Hope this helps, Rui Barradas Citando Matthias Gondan <matthias-gondan at gmx.de>:> Dear R developers, > > sessionInfo() below > > Please have a look at the following two versions of the same function: > > 1. Intended behavior: > >> Su1 = function(u=100, l=u, mu=0.53, sigma2=4.3^2) > + { > + print(c(u, l, mu)) # here, l is set to u?s value > + u = u/sqrt(sigma2) > + l = l/sqrt(sigma2) > + mu = mu/sqrt(sigma2) > + print(c(u, l, mu)) > + } >> >> Su1() > [1] 100.00 100.00 0.53 > [1] 23.2558140 23.2558140 0.1232558 > > In the first version, both u and l are correctly divided by 4.3. > > 2. Strange behavior: > >> Su2 = function(u=100, l=u, mu=0.53, sigma2=4.3^2) > + { > + # print(c(u, l, mu)) > + u = u/sqrt(sigma2) > + l = l/sqrt(sigma2) # here, l is set to u?s value > + mu = mu/sqrt(sigma2) > + print(c(u, l, mu)) > + } >> >> Su2() > [1] 23.2558140 5.4083288 0.1232558In the second version, the print> function is commented out, so the variable u is > copied to l (lowercase L) at a later place, and L is divided twice by 4.3. > > Is this behavior intended? It seems strange that the result depends > on a debugging message. > > Best wishes, > > Matthias> sessionInfo() > R version 3.4.1 (2017-06-30) > Platform: x86_64-w64-mingw32/x64 (64-bit) > Running under: Windows >= 8 x64 (build 9200) > > Matrix products: default > > locale: > [1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 > LC_MONETARY=German_Germany.1252 > [4] LC_NUMERIC=C LC_TIME=German_Germany.1252 > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > > loaded via a namespace (and not attached): > [1] compiler_3.4.1 tools_3.4.1 > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Another way to avoid the problem is to not redefine variables that are arguments. E.g.,> Su3 <- function(u=100, l=u, mu=0.53, sigma2=4.3^2, verbose){ if (verbose) { print(c(u, l, mu)) } uNormalized <- u/sqrt(sigma2) lNormalized <- l/sqrt(sigma2) muNormalized <- mu/sqrt(sigma2) c(uNormalized, lNormalized, muNormalized) }> Su3(verbose=TRUE)[1] 100.00 100.00 0.53 [1] 23.2558140 23.2558140 0.1232558> Su3(verbose=FALSE)[1] 23.2558140 23.2558140 0.1232558 Not redefining variables at all makes debugging easier, although it may waste space. Bill Dunlap TIBCO Software wdunlap tibco.com On Sat, Sep 2, 2017 at 10:33 AM, <ruipbarradas at sapo.pt> wrote:> Hello, > > One way of preventing that is to use ?force. > Just put > > force(l) > > right after the commented out print and before you change 'u'. > > Hope this helps, > > Rui Barradas > > > > Citando Matthias Gondan <matthias-gondan at gmx.de>: > > > Dear R developers, >> >> sessionInfo() below >> >> Please have a look at the following two versions of the same function: >> >> 1. Intended behavior: >> >> Su1 = function(u=100, l=u, mu=0.53, sigma2=4.3^2) >>> >> + { >> + print(c(u, l, mu)) # here, l is set to u?s value >> + u = u/sqrt(sigma2) >> + l = l/sqrt(sigma2) >> + mu = mu/sqrt(sigma2) >> + print(c(u, l, mu)) >> + } >> >>> >>> Su1() >>> >> [1] 100.00 100.00 0.53 >> [1] 23.2558140 23.2558140 0.1232558 >> >> In the first version, both u and l are correctly divided by 4.3. >> >> 2. Strange behavior: >> >> Su2 = function(u=100, l=u, mu=0.53, sigma2=4.3^2) >>> >> + { >> + # print(c(u, l, mu)) >> + u = u/sqrt(sigma2) >> + l = l/sqrt(sigma2) # here, l is set to u?s value >> + mu = mu/sqrt(sigma2) >> + print(c(u, l, mu)) >> + } >> >>> >>> Su2() >>> >> [1] 23.2558140 5.4083288 0.1232558 >> > In the second version, the print > >> function is commented out, so the variable u is >> copied to l (lowercase L) at a later place, and L is divided twice by 4.3. >> >> Is this behavior intended? It seems strange that the result depends on a >> debugging message. >> >> Best wishes, >> >> Matthias >> > > > sessionInfo() >> R version 3.4.1 (2017-06-30) >> Platform: x86_64-w64-mingw32/x64 (64-bit) >> Running under: Windows >= 8 x64 (build 9200) >> >> Matrix products: default >> >> locale: >> [1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 >> LC_MONETARY=German_Germany.1252 >> [4] LC_NUMERIC=C LC_TIME=German_Germany.1252 >> >> attached base packages: >> [1] stats graphics grDevices utils datasets methods base >> >> loaded via a namespace (and not attached): >> [1] compiler_3.4.1 tools_3.4.1 >> >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posti >> ng-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posti > ng-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Yes, this is intended behavior, and it has everything to do with where the parameters are first referenced and nothing to do with debugging. -- Sent from my phone. Please excuse my brevity. On September 2, 2017 10:22:22 AM PDT, Matthias Gondan <matthias-gondan at gmx.de> wrote:>Dear R developers, > >sessionInfo() below > >Please have a look at the following two versions of the same function: > >1. Intended behavior: > >> Su1 = function(u=100, l=u, mu=0.53, sigma2=4.3^2) >+ { >+ print(c(u, l, mu)) # here, l is set to u?s value >+ u = u/sqrt(sigma2) >+ l = l/sqrt(sigma2) >+ mu = mu/sqrt(sigma2) >+ print(c(u, l, mu)) >+ } >> >> Su1() >[1] 100.00 100.00 0.53 >[1] 23.2558140 23.2558140 0.1232558 > >In the first version, both u and l are correctly divided by 4.3. > >2. Strange behavior: > >> Su2 = function(u=100, l=u, mu=0.53, sigma2=4.3^2) >+ { >+ # print(c(u, l, mu)) >+ u = u/sqrt(sigma2) >+ l = l/sqrt(sigma2) # here, l is set to u?s value >+ mu = mu/sqrt(sigma2) >+ print(c(u, l, mu)) >+ } >> >> Su2() >[1] 23.2558140 5.4083288 0.1232558>In the second version, the print >function is commented out, so the variable u is >copied to l (lowercase L) at a later place, and L is divided twice by >4.3. > >Is this behavior intended? It seems strange that the result depends on >a debugging message. > >Best wishes, > >Matthias>>>> sessionInfo() >R version 3.4.1 (2017-06-30) >Platform: x86_64-w64-mingw32/x64 (64-bit) >Running under: Windows >= 8 x64 (build 9200) > >Matrix products: default > >locale: >[1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 >LC_MONETARY=German_Germany.1252 >[4] LC_NUMERIC=C LC_TIME=German_Germany.1252 > >attached base packages: >[1] stats graphics grDevices utils datasets methods base > > >loaded via a namespace (and not attached): >[1] compiler_3.4.1 tools_3.4.1 > > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
This is exactly as expected. See section 4.3.3 of the R Language definition or google around on "R Lazy Evaluation" for details. You should not "expect" R's semantics to be the same as other languages with which you may be familiar. Spending time with a good tutorial or two should help you sort out points of similarity and differences. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sat, Sep 2, 2017 at 10:22 AM, Matthias Gondan <matthias-gondan at gmx.de> wrote:> Dear R developers, > > sessionInfo() below > > Please have a look at the following two versions of the same function: > > 1. Intended behavior: > > > Su1 = function(u=100, l=u, mu=0.53, sigma2=4.3^2) > + { > + print(c(u, l, mu)) # here, l is set to u?s value > + u = u/sqrt(sigma2) > + l = l/sqrt(sigma2) > + mu = mu/sqrt(sigma2) > + print(c(u, l, mu)) > + } > > > > Su1() > [1] 100.00 100.00 0.53 > [1] 23.2558140 23.2558140 0.1232558 > > In the first version, both u and l are correctly divided by 4.3. > > 2. Strange behavior: > > > Su2 = function(u=100, l=u, mu=0.53, sigma2=4.3^2) > + { > + # print(c(u, l, mu)) > + u = u/sqrt(sigma2) > + l = l/sqrt(sigma2) # here, l is set to u?s value > + mu = mu/sqrt(sigma2) > + print(c(u, l, mu)) > + } > > > > Su2() > [1] 23.2558140 5.4083288 0.1232558 > In the second version, the print function is commented out, so the > variable u is > copied to l (lowercase L) at a later place, and L is divided twice by 4.3. > > Is this behavior intended? It seems strange that the result depends on a > debugging message. > > Best wishes, > > Matthias > > > > sessionInfo() > R version 3.4.1 (2017-06-30) > Platform: x86_64-w64-mingw32/x64 (64-bit) > Running under: Windows >= 8 x64 (build 9200) > > Matrix products: default > > locale: > [1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 > LC_MONETARY=German_Germany.1252 > [4] LC_NUMERIC=C LC_TIME=German_Germany.1252 > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > > loaded via a namespace (and not attached): > [1] compiler_3.4.1 tools_3.4.1 > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > and provide commented, minimal, self-contained, reproducible code.[[alternative HTML version deleted]]
Mathias, If it's any comfort, I appreciated the example; 'expected' behaviour maybe, but a very nice example for staff/student training! S Ellison> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Matthias > Gondan > Sent: 02 September 2017 18:22 > To: r-help at r-project.org > Subject: [R] Strange lazy evaluation of default arguments > > Dear R developers, > > sessionInfo() below > > Please have a look at the following two versions of the same function: > > 1. Intended behavior: > > > Su1 = function(u=100, l=u, mu=0.53, sigma2=4.3^2) > + { > + print(c(u, l, mu)) # here, l is set to u?s value > + u = u/sqrt(sigma2) > + l = l/sqrt(sigma2) > + mu = mu/sqrt(sigma2) > + print(c(u, l, mu)) > + } > > > > Su1() > [1] 100.00 100.00 0.53 > [1] 23.2558140 23.2558140 0.1232558 > > In the first version, both u and l are correctly divided by 4.3. > > 2. Strange behavior: > > > Su2 = function(u=100, l=u, mu=0.53, sigma2=4.3^2) > + { > + # print(c(u, l, mu)) > + u = u/sqrt(sigma2) > + l = l/sqrt(sigma2) # here, l is set to u?s value > + mu = mu/sqrt(sigma2) > + print(c(u, l, mu)) > + } > > > > Su2() > [1] 23.2558140 5.4083288 0.1232558 > In the second version, the print function is commented out, so the variable u > is copied to l (lowercase L) at a later place, and L is divided twice by 4.3. > > Is this behavior intended? It seems strange that the result depends on a > debugging message. > > Best wishes, > > Matthias > > > > sessionInfo() > R version 3.4.1 (2017-06-30) > Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows >= 8 > x64 (build 9200) > > Matrix products: default > > locale: > [1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 > LC_MONETARY=German_Germany.1252 > [4] LC_NUMERIC=C LC_TIME=German_Germany.1252 > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > > loaded via a namespace (and not attached): > [1] compiler_3.4.1 tools_3.4.1 > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.******************************************************************* This email and any attachments are confidential. Any use, copying or disclosure other than by the intended recipient is unauthorised. If you have received this message in error, please notify the sender immediately via +44(0)20 8943 7000 or notify postmaster at lgcgroup.com and delete this message and any copies from your computer and network. LGC Limited. Registered in England 2991879. Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK
Dear S Ellison, Thanks for the flowers! Indeed, I was actually considering to use it in my own teaching material, as well. With rounded numbers, of course. Though I am still slightly disturbed about this feature. I thought, now it is the time to switch to Python, but that?s even worse, see here: def add_elem(List=[]): List.append('elem') return List>>> add_elem([2])[2, 'elem']>>> add_elem()['elem']>>> add_elem()['elem', 'elem'] <<<<<<<<<<< why on earth does this happen? [it?s documented behavior, but still?] So, I?ll stick with R. Still 25 years or so until retirement, but I?ll survive, even without crossreferenced default arguments. Best wishes, Matthias Von: S Ellison Gesendet: Dienstag, 5. September 2017 16:17 An: Matthias Gondan; r-help at r-project.org Betreff: RE: [R] Strange lazy evaluation of default arguments Mathias, If it's any comfort, I appreciated the example; 'expected' behaviour maybe, but a very nice example for staff/student training! S Ellison> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Matthias > Gondan > Sent: 02 September 2017 18:22 > To: r-help at r-project.org > Subject: [R] Strange lazy evaluation of default arguments > > Dear R developers, > > sessionInfo() below > > Please have a look at the following two versions of the same function: > > 1. Intended behavior: > > > Su1 = function(u=100, l=u, mu=0.53, sigma2=4.3^2) > + { > + print(c(u, l, mu)) # here, l is set to u?s value > + u = u/sqrt(sigma2) > + l = l/sqrt(sigma2) > + mu = mu/sqrt(sigma2) > + print(c(u, l, mu)) > + } > > > > Su1() > [1] 100.00 100.00 0.53 > [1] 23.2558140 23.2558140 0.1232558 > > In the first version, both u and l are correctly divided by 4.3. > > 2. Strange behavior: > > > Su2 = function(u=100, l=u, mu=0.53, sigma2=4.3^2) > + { > + # print(c(u, l, mu)) > + u = u/sqrt(sigma2) > + l = l/sqrt(sigma2) # here, l is set to u?s value > + mu = mu/sqrt(sigma2) > + print(c(u, l, mu)) > + } > > > > Su2() > [1] 23.2558140 5.4083288 0.1232558 > In the second version, the print function is commented out, so the variable u > is copied to l (lowercase L) at a later place, and L is divided twice by 4.3. > > Is this behavior intended? It seems strange that the result depends on a > debugging message. > > Best wishes, > > Matthias > > > > sessionInfo() > R version 3.4.1 (2017-06-30) > Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows >= 8 > x64 (build 9200) > > Matrix products: default > > locale: > [1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 > LC_MONETARY=German_Germany.1252 > [4] LC_NUMERIC=C LC_TIME=German_Germany.1252 > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > > loaded via a namespace (and not attached): > [1] compiler_3.4.1 tools_3.4.1 > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.******************************************************************* This email and any attachments are confidential. Any use...{{dropped:12}}