s-news
[Top] [All Lists]

[S] &&

To: s-news@wubios.wustl.edu
Subject: [S] &&
From: Rolf Turner <rolf@math.unb.ca>
Date: Tue, 26 Sep 2000 17:13:28 -0300 (ADT)
Sender: owner-s-news@wubios.wustl.edu

Winfred Lambert wrote:

>  Per my recent email, Shawn Boles told me to try && instead of &, and I
>  noticed in my script that I used $ instead of & (that'll do it every
>  time!).  My mistake and I'm sorry to waste y'alls time.  Also, I tried
>  && and it didn't work, FYI.

This is a trap that I fall into repeatedly --- the  double logical
connectives (&& and ||) work only with scalar logicals.  (Why can't I
remember that?) The implication of using the double symbol rather
than the single is that the second argument is evaluated ***only***
if it is really needed.  For && the second argument is evaluated only
if the first argument is true.  So you can say e.g.

                if( x > 0 && log(x) < 3)

without things falling over when x is negative.

(For x || y, y is evaluated only if x is false.)

So && has absolutely no bearing on the orginal problem.

Superficially it would seem to be possible to ``vectorize'' && and ||;
after thinking about it a little more deeply, I've decide that it
probably ***isn't*** possible.  I think that the scalar operations
work because of the ``lazy evaluation'' of arguments in S functions.

E.g. one could define a shagganappi version of ``&&'' as

aa <- function(x,y) {if(!x) F else x & y}

Then for u <- 42, aa(u > 0, log(u) < 3) evaluates to F
     for u <- 20, aa(u > 0, log(u) < 3) evaluates to T
     for u <- -1, aa(u > 0, log(u) < 3) evaluates to F

and nothing falls over.  For the vector case you would somehow
have to make each entry of y into a separate argument which would
get ***evaluated*** only the corresponding entry of x were T.
But once you touch the argument y, it gets evaluated, and if
some entries result in errors, disaster strikes immediately.

Is there really no way, by tweaking all the subtle machinery of calls
and expressions and eval and parsing and deparsing and ... all that
stuff that I don't understand, to get around this impass?

                                        cheers,

                                                Rolf Turner
                                                rolf@math.unb.ca
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>
  • [S] &&, Rolf Turner <=