Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^3: Control Structures

by mstone (Deacon)
on May 11, 2005 at 03:40 UTC ( [id://455850]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Control Structures
in thread Control Structures

On the off-chance that this does work, the code is so tricky you probably shouldn't even think about using it in real life.

I'm willing to accept the possibility that:

S while (!B): T;

is pronounced: "use T as the continue block for a while() loop that controls S." If it doesn't, then it seems like the colon should be a semicolon, and you're looping over S until B returns TRUE, then calling T.. which a loop-and-a-half doesn't do.

I'm also willing to consider the possibility that the whole statement is somehow the conditional that controls the loop() statement, and could thus be written like so:

loop (; S while (!B): T ; repeat) { }

but I'm damned if I can see how the conditional in the while() loop drops through to control the loop() statement, and I have no idea why you'd want to call repeat as the loop() statement's continuation routine.

The best way I know to express the loop-and-a-half is:

while (1) { S; # make a calculation last if (B); # drop out when the result is right T; # adjust the parameters for another try }

which is, at very least, easier to read. The fact that expressing the idea requires a last statement goes right to the heart of the fight that made Dijkstra's Use of Go To Considered Harmful so infamous.

According to the key theory of structured programming (I don't reall who did the proof and don't have my references with me right now), you can write any program with nested function calls (where each function has a single entry point and a single exit point), while() loops, and if() statements. The problem is that some forms of logic are extremely ugly when written using only those tools.

We've solved those aesthetic problems by adding things like the next, last and redo statements, continue blocks, else and elsif() statements, the capacity to exit a function from more than one place, and so on. Technically, those tools exceed the 'minimal effective system' necessary to write programs, but they don't violate the spirit of structured programming, and they make the code a heck of a lot easier to read.

Replies are listed 'Best First'.
Re^4: Control Structures
by hossman (Prior) on May 11, 2005 at 17:25 UTC

    To be honest, I'm totally lost in what you are saying up to "The best way I know to express the loop-and-a-half is:" ... from there on, I'm with you 100%. and you're right, this...

    while (1) { S; last if (B); T; }

    ...is a perfectly valid way of expressing the construct in Perl(5). But I'd like something:

    • That doesn't require an explict "last" or "goto" inthe condition test
    • that has less neccessary syntactic cruft (ie: the "(1)" on the while, and the "if" in the condition test
    • forces S and T to be seperate scopes -- admitadly, this is just my own personal view on what *seems right* ... other people may not feel hat adds any value

    hence my desire to have...

    do { S; } while (!B) { T; }

    ...in perl5, which when S or T are No-Ops, breaks down to either...

    do { S; } while (!B); # OR while (!B) { T; }

    but the meaning of "do" changes in P6, hence...

    loop { S; } while (!B) { T; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://455850]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-16 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found