From hb at biostat.ucsf.edu  Sat Jan 12 18:50:21 2013
From: hb at biostat.ucsf.edu (Henrik Bengtsson)
Date: Sat, 12 Jan 2013 09:50:21 -0800
Subject: [Rd] Tricking Promises into Sending Info Via Args into Caller
In-Reply-To: <C855DD54-8358-4E1D-879A-97D98FDED513@gmail.com>
References: <CAP01uRnT_Lvw_97GAhXP7xOo6UjsXCTEVPf-H=OAQfWqwd0h1Q@mail.gmail.com>
	<C855DD54-8358-4E1D-879A-97D98FDED513@gmail.com>
Message-ID: <CAFDcVCQuqhLES5YpRqi-6-6LP0hac7sNBB92Eoq_o-2s0n6zPw@mail.gmail.com>

In the spirit of this, but AFAIK not due to lazy evaluation, here's
another illustration why it's easy to mistakes when doing "inline"
assignments:

> x <- 0
> TRUE && (x <- 1)
[1] TRUE
> x
[1] 1

> FALSE && (x <- 2)
[1] FALSE
> x
[1] 1

> (x <- 3) && FALSE
[1] FALSE
> x
[1] 3

> FALSE & (x <- 4)
[1] FALSE
> x
[1] 4

/Henrik

On Sat, Jan 12, 2013 at 9:19 AM, peter dalgaard <pdalgd at gmail.com> wrote:
>
> On Jan 12, 2013, at 17:02 , Gabor Grothendieck wrote:
>
>> The is.pos function below results in the variable, out, being set to
>> TRUE if the first argument to is.pos is positive and to FALSE
>> otherwise.
>>
>> It does this without using the return value or using scoping tricks to
>> reach into the caller.  Instead it tricks the promise into
>> communicating one bit of information upwardly from the function to its
>> caller via the second argument.
>>
>> One would have thought this to be impossible.  Is this intended behavior?
>
> Yes, this is a generic consequence of lazy evaluation: delayed and unpredictable side effects. Whether it is desirable is an open issue; it is the sort of thing that creates serious headaches for compiler constructors, but it is pretty much unavoidable once you include the lazy eval feature.
>
>>
>> is.pos <- function(i, x) { if (i > 0) x; NULL }
>>
>> # in this example actual arg1 of is.pos is positive
>> out <- FALSE
>> is.pos(1, out <- TRUE)
>> out # TRUE
>>
>> # in this example actual arg1 of is.pos is negative
>> out <- FALSE
>> is.pos(-1, out <- TRUE)
>> out # FALSE
>>
>> --
>> Statistics & Software Consulting
>> GKX Group, GKX Associates Inc.
>> tel: 1-877-GKX-GROUP
>> email: ggrothendieck at gmail.com
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] Tricking Promises into Sending Info Via Args into Caller
Henrik Bengtsson