[6517] in s-news-athena

home help back first fref pref prev next nref lref last post

Where to put the + operator when continuing a line

daemon@ATHENA.MIT.EDU (Steven P. Millard)
Mon Feb 27 00:18:15 1995

Date: 26 Feb 95 23:51:34 EST
From: "Steven P. Millard" <74273.3130@compuserve.com>
To: S-news <S-news@utstat.toronto.edu>

Hi there,

Just a word of warning:  when writing functions, do not continue an 
expression on the next line with the "+" or "-" operators (put the 
+ or - at the end of the first line), otherwise you can get unexpected 
results.  Here is a simplified example of one that got 
me a couple of weeks ago.  Compare the results of fcn1() and
fcn2(): 

Source files look like this:
---------------------------

"fcn1" <- function(x, y, z) 
{
	value <- x + y
		+ z
	value
}

"fcn2" <- function(x, y, z)
{
	value <- x + y + 
		z
	value
}



Inside S-PLUS, the functions look like this:
-------------------------------------------
> fcn1
function(x, y, z)
{
	value <- x + y
 + z
	value
}

> fcn2
function(x, y, z)
{
	value <- x + y + z
	value
}

Results look like this:
----------------------
> fcn1(1,2,3)
[1] 3

> fcn2(1,2,3)
[1] 6

Here is Bill Dunlap's (of StatSci) explanation:
----------------------------------------------
Splus will parse
        a
        +b
as two expressions, "a" and "+b", but will parse
        a+
        b
as one expression, "a+b".  This is its intended behavior so
one can continue lines without special continuation or
expression termination characters.  Whenever it reaches a newline
(or a semicolon, there is an expression terminator if one wants
to use it) it checks to see if the text it has so far can be
construed to be a complete expression.  If so it says this
is an expression and the next line starts a new expression.
If not, it assumes the next line continues the current expression.
It does not look at the next line to determine whether the current
line is a complete expression or not (in interactive use, the
next line doesn't exist when that decision must be made).

--------------------------------------------------------------------------

--Steve M.



(~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~)
(   _____________                                           )
(   |    ***    |  Steven P. Millard                        )
(   |     *     |                                           )
(   | *   *   * |  P robability,                            )
(   |   * * *   |  S tatistics &                            )
(   |     *     |  I nformation                             )
(   |     *     |                                           )
(   |    ***    |  7723 44th Avenue NE                      )
(   |___________|  Seattle, WA 98115  USA                   )
(                                                           )
(                  Voice/FAX:  (206) 528 - 4877             )
(                  E-mail:     74273,3130@CompuServe.COM    ) 
(___________________________________________________________)








home help back first fref pref prev next nref lref last post