Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: RE: st: RE: Irritating if...else question for mata vs. stata


From   wgould@stata.com (William Gould, StataCorp LP)
To   statalist@hsphsun2.harvard.edu
Subject   Re: RE: st: RE: Irritating if...else question for mata vs. stata
Date   Mon, 07 Jun 2010 10:57:41 -0500

Thomas Jacobs <thomasjacobs@gmail.com> writes, 

> Here is how the final part of the mata code appears utilizing the 
> belief that mata if blocks need not be followed by empty else blocks
> and the error it generates:
> 
> : if (FirstCDXDay > 0) {
> >
> >         CDXAdjSizePowVRight = vec(CountCDXAdjSizePowRight')
> >         CDXMktSizePowVRight = vec(CountCDXMktSizePowRight')
> >         CDXAdjSizePowVLeft = vec(CountCDXAdjSizePowLeft')
> >         CDXMktSizePowVLeft = vec(CountCDXMktSizePowLeft')
> >
> > }
> >
> > end

unexpected end of line
(117 lines skipped)

Thomas goes on to note that when he "adds back the heretofore extraneous else
block and the program continues on to successful completion":

> : if (FirstCDXDay > 0) {
> >
> >         CDXAdjSizePowVRight = vec(CountCDXAdjSizePowRight')
> >         CDXMktSizePowVRight = vec(CountCDXMktSizePowRight')
> >         CDXAdjSizePowVLeft = vec(CountCDXAdjSizePowLeft')
> >         CDXMktSizePowVLeft = vec(CountCDXMktSizePowLeft')
> >
> > }
> > else{
> > }

Thomas concludes, 

> Any suggestions for what I may be missing or how I might better
> understand what is going on here?  [...]
> [There] certainly there must be some explanation for such behavior.
> It has happened to me before and I make a habit of posting empty
> else blocks all over the place and 


Why this is happening
---------------------

Thomas is entering 

      : if (<exp>) { 
      >         ...
      > }
      > _

interactively or in a do-file.  Mata is trying to figure out when
Thomas is done entering the the line.  Mata says to itself, "Thomas
start with -if-, entered an expression, and the body.  I understand
that.  Possibly Thomas will have an -else- clause following this.
Oops, Thomas hit Enter gain.  I wonder why Thomas wants all that white
space?  Oh well, possibly Thomas will have an -else- clause following
the -if- and white space.  Hey!  Where is Thomas?  He hasn't typed
anything in while.  Ah, there he is, reviewing the manuals again.
Smart.  But getting back to the problem at hand, possibly Thomas will
have an -else- clause following the -if-, white space, and manual
review.  What?  Even more white space?  Okay.  Perhaps Thomas will
have an -else- clause following the -if-, white space, manual review,
and even more white space.  Boy am I glad I don't have the microphone
on.  Thomas looks really mad.  I wonder why?  Oh well, perhaps Thomas
will have an -else- clause following the -if-, white space, manual
reivew, more white space, and getting over his anger.  Oh no, Thomas
is crying.  I've seen him do that before.  Oh well, possibly Thomas
will have an -else- clause following the -if-, white space, manual
review, more white space, getting over his anger, and now the bad news
he obviously received.  I'll give him another ">" prompt.  Maybe that
will make him feel better."

Mata is a one-token look-ahead compiler, which is to say that in cases
such as -if- possibly followed by -else-, Mata has to wait and see
what follows before he it can interpret and compile what's typed.

If Thomas had typed 

      : if (<exp>) { 
      >         ...
      > }
      > x = 3 

      : _

Mata would have quickly figure out that there is no -else- and Thomas 
would never have known that Mata was looking ahead to determine whether 
an -else- would follow the -if-.  Usually, -if-s appear in function 
definitions, and no one notices when Mata looks ahead.  When 
you type

        : function xxx() 
        > {
        >        ...
        > }

        : _

Mata does not look seriously inside the braces until you enter the 
final closing brace.  Thus, Mata can look ahead all it wants and you 
don't even know it.

Most real compilers don't have a a line-by-line interactive mode.
Mata does.

In line-by-line interatactive mode, most people don't type -if-.  That's 
why we've been able to go so long with no else noticing when Mata 
looks ahead.


Another solution
----------------

Thomas's solution has been to type -else- Mata is watching for:


      : if (<exp>) { 
      >         ...
      > }
      > else {
      > }

      : _

That was a good solution in that it got Thomas through the problem.
Mata however, is not demanding that there be an -else-, Mata is just 
waiting to see if there is an -else.  Another solution is to type
a semicolon to the end the line:

     : if (<exp>) { 
     >         ...
     > } ;

     : _

Mata does not require semicolons to end lines as C does, but Mata allows 
them, and when typed, Mata takes them seriously.

When Mata was locked into its loop "Possibly Thomas wll have an 
-else- clause following the -if-", had Thomas typed an -;- 
Mata would have immediately thought, "Evidently not.  This is fun".



Cheers, 

-- Bill
wgould@stata.com
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index