Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Unexpected end of file in mata.
From
Amadou DIALLO <[email protected]>
To
[email protected]
Subject
Re: st: Unexpected end of file in mata.
Date
Thu, 23 Jan 2014 16:44:10 +0100
Many thanks Bill for this enlightening explanation of semicolon and if.
I'd have never guessed and in fact, in my mind, semicolon is usable
only in the stata's delimiter mode. So, it is a good thing mata is
more flexible. But, the semicolon is not only required in interactive
mode. It is also required in do or ado files too.
Based on your codes, I've further pursued my learning. I'm learning
fun, but mata seems to be more challenging than stata. Consider the
code below:
. mata
------------------------------------------------- mata (type end to
exit) ---------------
: i = 1
: if (i == 1) {
> i
> }
> ;
1
: if (i = 1) {
> i
> }
> ;
1
: if (i = 1) {
> i
> }
> else "False"
1
: i = 2
: i
2
: if (i = 1) {
> i
> }
> else "False"
1
: i
1
: i = 2
: if (i == 1) {
> i
> }
> else "False"
False
: if (i == 1) {
> i
> }
> else i
2
:
: end
-----------------------------------------------------------------------------------------
I have 2 remarks:
- I am puzzled to see that mata accepts both (i=1) and (i==1). Because
it is that flexible?
- I don't understand why the command if (i = 1) { reassigns
automatically i = 1 even if i = 2. What is happening here?
On the hand, based on a discussion with Nick, I was asking why we
don't have if qualifier both ways as in Stata (if foreign == 1 ...
versus su mpg if foreign == 1 for example). Nick disagreed but indeed
it s a matter of taste. I think having if parsed in mata the same way
in stata could be helpful. Conceptually, that should be doable, I
guess.
Finally, I have 2 or 3 other small frustrations.
1. The almost impossibility to trace down a mata code when it crashes.
In mata v12 manual p.907, you gave a tracelog example. However, if the
function solve() itself is very big (some millions of line), how do
you detect the error? Maybe the tracelog should indicate the exact
line where the problem arises (as in C++ compiler or VB).
2. Are there any equivalent command such as "capture" and "assert" or
other useful commands in mata?
I stop here, but I think I'll more and more migrate to mata.
Have a good night.
Bachir.
2014/1/22, William Gould, StataCorp LP <[email protected]>:
> Amadou Diallo <[email protected]> writes,
>
>> I am still learning (and enjoying mata !!!), but frankly the entry
>> cost is big. I don't understand why the following code is crashing.
>
> and he shows some examples, one of which is
>
> . mata
> --------------------------------- mata (type end to exit) ---
> : i = 1
>
> : if (i == 1) {
> > i
> > }
> > end
>
> unexpected end of line
> -------------------------------------------------------------
>
> . _
>
> What's happening here is going to take some explaining. Before I do
> that, let me mention that Mata is designed as a programming language
> that in addition, has a interactive mode. The interactive mode mostly
> works exactly as you would expect. The exception is when you use
> something that is (or at least was intended) as purely a programming
> feature. In that case, the interactive mode can surprise you. That's
> what happened here.
>
> So one answer is, "Why do you want to use -if- interactively, anyway?"
>
> Amadou's answer will likely be, "because I'm trying out the features to
> better understand them. Isn't that one of the purposes of interactive
> mode?"
>
> Yes. I certainly aprpeciate Amadou's frustration.
>
> Alright, so let's delve into this case.
>
> The basic syntax of -if- is
>
> if (<exp>) {
> <anything>
> }
> else {
> <anything>
> }
>
> or, more densely
>
> if (<exp>) <anything>
> else <anything>
>
> where <anything> can include include braces, and must include braces
> if there is more than one statement to appear in the block.
>
> The -else- is optional. Amadou typed the syntax the without an -else-.
>
> Mata, however, is still waiting to see the -else- or to hear from
> Amadou that the statement is complete.
>
> Amadou tried to tell Mata that the statement was complete by typing
> -end-:
>
> . mata
> --------------------------------- mata (type end to exit) ---
> : i = 1
>
> : if (i == 1) {
> > i
> > }
> > end
>
> unexpected end of line
> -------------------------------------------------------------
>
> . _
>
> The -end- statement, however, means "exit Mata now". So Mata exited back
> to Stata, and Mata felt obligated to mention that Amadou was leaving
> Mata in the what Mata considered the middle of a line.
>
> So how was Amadou supposed to tell Mata that there is no more?
>
> He needed to type a semicolon:
>
> . mata
> --------------------------------- mata (type end to exit) ---
> : i = 1
>
> : if (i == 1) {
> > i
> > }
> > ;
> 1
>
> : _
>
> Mata is pretty relaxed about semicolons. Mostly, it figures out
> what they need to be by itself. In the formal programming language,
> it is nearly 100% successful in doing that. There is one case I'll
> tell you about shortly.
>
> Interactively, however, this was a case where Amadou needed to say
> "and there is no more", and anytime you want to say "and there is no
> more", you type a semicolon interactively.
>
> As I said, "Why do you want to use -if- interactively, anyway?"
>
> In a program, Mata wouldn't have needed the semicolon, but it
> wouldn't hurt. If I wrote the program
>
> function myfcn(i)
> {
> if (i==1) {
> i
> return
> }
> i = i + 1
> "I incremented i for you"
> }
>
> Mata would ahve figured out the semiclon after the close brace and before
> the -i = i + 1- statement. It would ahve figured it out because
> Mata would ahve seen -i = i + 1- and said, "well, that's not -else-, so
> the -if- statement is complete."
>
> Interactively, Mata was trying to be nice. Watch:
>
> : if (i == 1) {
> > i
> > }
> > <- I pressed Return
> > <- Darn, another prompt
> > <- and another
> > <- What's happening is that Mata is
> > <- saying, "one more change to type -else-"
> >
> > ; <- Finally, I type semicolon -- I'm done
> 1 <- okay, says Mata, I'll execute.
>
> : _
>
> So what's the one programming case where you have to type semicolon.
> It's this:
>
> function forexample(...)
> {
> ...
> for (i=1; mysubroutine(...)!=0; i++) ;
> mycloseout(...)
> ...
> }
>
> In this program, I'm calling mysubroutine() over and over again
> until mysubroutine() returns 0, and I'm couting the number of times
> I called it.
>
> Imagine the above code without the semicolon.
>
> The syntax of -for- is
>
> for (<exp>; <exp>; <exp>) <anything>
>
> In this case, I want <anything> to be nothing. I have to type a
> semicolon. if I didn't, the next line, -mycloseout(...)-, would have
> been taken as the <anything> and would have been executed each time
> mysubroutine() did not return 0.
>
> Well, I said there was one case, but there are two. In my defense,
> the second is really the same case as the first. It concerns -while-.
> If I didn't want to count the number of times I called mysubroutine(),
> I could have coded
>
> function forexample(...)
> {
> ...
> while (mysubourinte()!=0) ;
> mycloseout(...)
> ...
> }
>
> Note the semicolon. Without it, mycloseout() would have been considered
> part of the body for while().
>
> Well, I I said there were two cases, but there are actually three.
> In my defense, no one ever codes the third case. It's this one:
>
> function lastexample(...)
> {
> ...
> if (x==1) ;
> else y = x + 2
> ...
> }
>
> Note the semicolon. I made the body of the -if- empty. Most of us
> would code this as
>
> function lastexample(...)
> {
> ...
> if (x!=1) y = x + 2
> ...
> }
>
> So let me summarize:
>
> 1. Semicolon (;) may be used interactively or inside programs.
> You use it to say, "and that's the end of the statement".
> You are required to use it when Mata might otherwise be
> confused and think the current statement could continue.
> You may use whenever a statement is done.
>
> 2. -end- is the instruction, "Get me out of Mata right now."
>
> I hope this helps.
>
> -- Bill
> [email protected]
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/faqs/resources/statalist-faq/
> * http://www.ats.ucla.edu/stat/stata/
>
--
Amadou B. DIALLO, PhD.
Senior Economist, AfDB.
[email protected]
+21671101789
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/