Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Control Structures

by hossman (Prior)
on May 10, 2005 at 07:27 UTC ( [id://455454]=note: print w/replies, xml ) Need Help??


in reply to Re: Control Structures
in thread Control Structures

Dijkstra aparently coined this: "looping n and a half times" (or the "loop and a half" as some people have shortened it) back in 1973. Knuth mentioned it as one of the main reasons for using goto in his 1974 "Structured Programming with go to Statements" (which does not seem to be available online unless you are an ACM member) ...

A: S; if B then goto Z fi; T; goto A; Z:

He mentions several alternatives that he finds inferor to the goto version for various reasons, and credits Ole-JOhan Dahl as proposing a syntax he really like -- which frankly I think kicks ass, and plan on writting as a P6 macro (I think macro is the right word)...

loop; S while !B: T; repeat;
or in the more practical perlish way...
loop { S; } while (!B) { T; }

Replies are listed 'Best First'.
Re^3: Control Structures
by mstone (Deacon) on May 11, 2005 at 03:40 UTC

    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.

      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; }
Re^3: Control Structures
by BUU (Prior) on May 10, 2005 at 20:36 UTC
    Er, perhaps I'm badly missunderstanding you, but wouldn't:
    loop;  S  while !B:  T;  repeat;
    Translate fairly literally in to:
    { S while !B; T; redo; }
    In which case I don't understand your "practical perlish way".
      S and T should be done on every iteration, except when B goes true, in which case the final T should be skipped. Your syntax seems to do a lot of S repetitions then one T, and repeats endlessly.

      --
      [ e d @ h a l l e y . c c ]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-04-24 08:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found