Skip to content

how to get or store the intermediate v?lues while running a function

33 messages · shanmuha boopathy, vincent@7d4.com, Martin Maechler +15 more

Messages 26–33 of 33

#
Well, this has been an interesting thread.  I guess my own perspective 
is warped, having never been a C programmer.  My native languages are 
FORTRAN, python, and R, all of which accept (or demand) a linefeed as a 
terminator, rather than a semicolon, and two of which are very 
particular about whitespace.

Accepting for a moment Ted's argument about wanting to compact his code, 
the problem as I understand it is that ";" is a statement separator, not 
a statement terminator, so that Ted really does need all those ";" in 
between his statements on a single line, but that the last one implies a 
NULL statement on every line.  Given that the ";" facilitates compacting 
lines in vi (or vim), how about when the code is compacted you try

1,$s/;$//

which will remove all the final trailing ";" leaving the other necessary 
";" separators.

Dave R.
(Ted Harding) wrote:

  
    
#
On 12/7/05, Dave Roberts <droberts at montana.edu> wrote:
But watch out for this:

x <- "abc;
def"
#
On Tue, Dec 06, 2005 at 04:21:01PM +0000, Patrick Burns wrote:
I don't understand. It would seem to me that in

    if (x > 0) y <- 4;
    else y <- 4.5e23;

it's pretty obvious that the "if" statement is terminated by the
semicolon at the end of the first line and that therefore, the "else"
on the next line is erroneous because it is not associated with any
"if".

At least, the version above fails consistently, i.e. regardless of
context. On the other hand, I've studied the R Language Definition for
quite some time before fully understanding why

    if (x > 0) y <- 4
    else y <- 4.5e23

works inside of a function (or other enclosing block) while it does not
work interactively (or at the top level of a script).

Best regards, Jan
#
Adaikalavan Ramasamy wrote:
In fact, I like the <- assignment operator, but tend to write code 
densely myself as that is the way I like to view it. As R formats the 
source code it displays in the typographic density approved by the 
gurus, it does seem a tidge authoritarian to insist that everyone adhere 
to the same coding style in private. After all, I don't whinge about 
having to scroll up and down like a yoyo when attempting to decipher 
other people's "spaced out" code.

I intentionally don't use semicolons as line terminators in R to 
forestall my C reflexes. The greatest difficulty I have with this sort 
of interference is between R and Tcl-Tk which, combined with the correct 
titre of fatigue, almost always leads to syntax errors. I did enjoy 
Ted's defense of the semicolon in R, as it emphasizes the diversity of 
styles upon which cooperative programming depends.

Jim
#
On 12/7/05, Jan T. Kim <jtk at cmp.uea.ac.uk> wrote:
Why is it obvious?

     if (x > 0) y = 4;
     else y = 4.5e23;

is perfectly legal C.
It fails for unrelated reasons.

   if (x > 0) { y <- 4; } else { y <- 4.5e23; }

doesn't fail.

-Deepayan
#
Please use an informative subject line.
On Wed, 7 Dec 2005 vincent at 7d4.com wrote:

            
Yes, those are the 'Bengtsson Coding Conventions'.  (He says 'our', but it 
appears to be a royal 'we'.)  Some contradict the primary documentation 
(e.g. for S3 generics where dispatch can usefully occur on particular 
named arguments), and the author seems unaware that underline is allowed 
in syntactic names (and is preferred to capitalization schemes by many).
I had already posted this in this thread: it is in sections 3.1 and 
Appendix B of `Writing R Extensions'.  (You seem unaware that this is a 
manual that ships with every copy of R, as you give a URL on CRAN for 
part of it.)

The style R itself uses to deparse code is canonical.  As in
+ abc=T;
+ 7+8;
+ }
function ()
{
     abc = T
     7 + 8
}

Voila, those distracting semicolons have disappeared! (R has removed the 
pointless empty statement which was specified at the end of the lines, 
between the semicolon and newline.)

Note how spaces are used to improve clarity.

Note that the use of T has not, as using T as a variable is legitimate R. 
The manual in 2.2.0 (but not 2.2.1 beta) says <- will be used for 
assignment, and indeed the equivalent _ was converted to <- whereas = is a 
different token (and postdates that manual section).  But it does say <- 
is preferred.

To quote:

   This tidied version is much easier to read, not least by other users
   who are used to the standard format.

and that is the main point, reinforced by Philippe's message.  If you are 
trying to communicate, make life easier for your readers.
#

        
DeepS> On 12/7/05, Jan T. Kim <jtk at cmp.uea.ac.uk> wrote:
>> On Tue, Dec 06, 2005 at 04:21:01PM +0000, Patrick Burns wrote:
>> > I don't put in extraneous ';' because I maybe get a
    >> > blister on my little finger.
    >> >
    >> > I suspect that those who find the semi-colons ugly in
    >> > R do not find them ugly in C.  I think the reason there
    >> > would be a visceral reaction  in R but not in C is that
    >> > there is a danger when using them in R that they really
    >> > mean something.
    >> >
    >> > We get questions on R-help often enough about why
    >> > code like:
    >> >
    >> > if(x > 0) y <- 4
    >> > else y <- 4.5e23
    >> >
    >> > doesn't work.
    >> >
    >> > If people habitually used semi-colons, those sorts of
    >> > questions would probably multiply.
    >> 
    >> I don't understand. It would seem to me that in
    >> 
    >> if (x > 0) y <- 4;
    >> else y <- 4.5e23;
    >> 
    >> it's pretty obvious that the "if" statement is terminated by the
    >> semicolon at the end of the first line and that therefore, the "else"
    >> on the next line is erroneous because it is not associated with any
    >> "if".

    DeepS> Why is it obvious?

    DeepS>    if (x > 0) y = 4;
    DeepS>    else y = 4.5e23;

    DeepS> is perfectly legal C.

Very good!  I'm glad we got here,  namely back to the original
subject, and now have seen a nice example of why it can be a bad
idea to maltreat the S language by extraneous ";" ..

    >> At least, the version above fails consistently, i.e. regardless of
    >> context.

    DeepS> It fails for unrelated reasons.

    DeepS> if (x > 0) { y <- 4; } else { y <- 4.5e23; }

    DeepS> doesn't fail.

    DeepS> -Deepayan

Thank you, Deepayan, Brian,
and all the others "in the knowning" who hadn't lost patience with
this thread yet... ;-) 
Martin

    >> On the other hand, I've studied the R Language Definition for
    >> quite some time before fully understanding why
    >> 
    >> if (x > 0) y <- 4
    >> else y <- 4.5e23
    >> 
    >> works inside of a function (or other enclosing block) while it does not
    >> work interactively (or at the top level of a script).
    >> 
    >> Best regards, Jan
    >> --
    >> +- Jan T. Kim -------------------------------------------------------+
    >> |             email: jtk at cmp.uea.ac.uk                               |
    >> |             WWW:   http://www.cmp.uea.ac.uk/people/jtk             |
    >> *-----=<  hierarchical systems are for files, not for humans  >=-----*
#
Prof Brian Ripley a ??crit :
Dear Prof Ripley,

I am certainly missing something. I did read
http://cran.r-project.org/doc/manuals/R-exts.html#Tidying-R-code
which is section 3.1 of `Writing R Extensions',
and also Appendix B of `Writing R Extensions'
which corresponds to the adress I have given in my previous message
http://cran.r-project.org/doc/manuals/R-exts.html#R-coding-standards

I have probably to change my glasses, but didn't read any
recommandation about semicolon usage there.
Apologies if I missed it.

 > (You seem unaware that this is a manual that ships with every
 > copy of R, as you give a URL on CRAN for part of it.)

Thanks for this point, but I'm aware about R docs on my PC.
The only reason I give URLs is by courtesy, for the convenience
of the interested readers, in order to give them direct access.