Pergunta

I have been doing some statistics work with Stata recently and not enjoying it very much.

It doesn't feel to me like it's a "proper" programming language: in particular I don't think there's a way to loop until a condition is met.

Am I right in my feeling, or is Stata truly Turing-complete?

Foi útil?

Solução

I've never heard of Stata before but the webpage brags that it has "if, while" and "looping and branching".

Wikibooks has this example:

local k = 1
file open myfile using toto.txt, read text
file read myfile line
while r(eof) == 0 {
    local k = `k' + 1
    di "`k' `line'"
    file read myfile line
    }
file close myfile

I don't know what "proper" programming language means but at first glance it definitely appears to be Turing-complete.

Outras dicas

A "proper" programming language in the sense that you could build a webpage or GUI with it? Of course not. But that's a bit extreme. You can certainly write loops with .ado and .do files; i would say it is turing complete.

@eric.a.booth: I think your example is strange. I'm not sure I've ever seen while { ... } else {...}

Also, note that Stata doesn't test the loop before you run it, and will allow itself to get caught in an infinite loop.

local x = 0
while `x'<5 {
   display `x' / 2
   local ++x
}

While you can use the -while-, -if, -else- commands to perform looping until a condition is met, it's usually a better idea in Stata to use the -foreach- or -forvalues- loops in their place.
So, instead of saying:

while "`1'" != "" {
<do something>
} 

or

if "`a'" == "" {
<do something>
}
else {
<do something else>
}

it's usually better (and more intuitive) to instead to do:

forvalues x = 1/100 {
<do something>
}

-- No -if-, -else-, or -break- conditions needed. See -help forvalues- or -help foreach- in Stata for details.


^NOTE: the while-else loop in my original post was removed--thanks for the heads-up, Keith. The -else- part was intended for the if{] else{} loop example only. Regardless, the point of my post wasn't to suggest the use of a while/else or if/else loop, it was that -foreach-/-forvalues- are usually a preferred approach.

Stata's ado language has all the usual conditional statements:

However, it is important that one does not confuse the two:

In addition, Stata's ado language loop constructs include:

Mata, Stata's matrix programming language also supports:

Consequently, both Stata's ado and mata programming languages appear to satisfy the criteria for being characterized as Turing complete.

It is important to note though that these are not general programming languages, but full-fledged statistical languages.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top