pervognsen,
@pervognsen@mastodon.social avatar

"The algorithm uses exactly the same terminology and is presented in bottom-up form. (If you prefer top-down design, please read the rest of this section backwards.)"

nicholas_saunders,

@pervognsen which text? epub?

pervognsen, (edited )
@pervognsen@mastodon.social avatar

These classic-era structured programmers really loved their sentinels and really hated their gotos/breaks/returns. You can't tell me that it's easier to prove the correctness of this with Hoare-Dijkstra-style assertions than if you had an early return; assertions flow across control flow edges just fine, and when you have multiple control flow predecessors your precondition is the logical disjunction of those in-flowing assertions. Bob Floyd used Hoare-style triples for flow charts before Hoare.

TomF,
@TomF@mastodon.gamedev.place avatar

@pervognsen Oh for fuck's sake...

TomF,
@TomF@mastodon.gamedev.place avatar

@pervognsen Like honestly my day job is working in a pre-existing large codebase, and the number of times I have to restrain myself from fixing some convoluted mess with a simple "goto" is easily >=3 per week.

If the code is scary, it should look scary. And goto is scary by design. You should be scared! But not pathologically phobic.

pervognsen,
@pervognsen@mastodon.social avatar

@TomF Well, in this case you'd just need a break or an early return. When you look at a lot of the old articles on GOTO vs structured programming, including Knuth's famous article, they're working in a world where neither break or early return is a thing in structured languages, or at least not considered admissible. Almost all Knuth's examples (as far as I remember) in favor of GOTO are solvable with single-level continue, break or early return. Labeled break/continue probably handles the rest.

pervognsen, (edited )
@pervognsen@mastodon.social avatar

@TomF Most of the remaining examples that "need" GOTO rather than labeled break/continue come from irreducible control flow graphs, usually from non-trivial finite state machines where instead of representing the state as a variable and using a loop with a switch on the state variable, you implicitly encode the state via the program counter and use direct GOTOs between states. Those cases do occur but pretty infrequently. The common "GOTO the error handler" example is just labeled break.

pervognsen,
@pervognsen@mastodon.social avatar

@TomF As an aside, modern compilers perform an optimization called jump threading that turns the above example of a loop + state switch into the equivalent machine code of the GOTO version. So from the perspective of the final machine code (which was Knuth's concern in that article), there's no advantage even in that remaining class of examples.

TomF,
@TomF@mastodon.gamedev.place avatar

@pervognsen I mean LLVM has no flow control constructs except for "goto" so yeah good job bending over backwards to avoid that, structured programming fetishists.

lritter,
@lritter@mastodon.gamedev.place avatar

@TomF @pervognsen SPIRV enforces structured control flow so what i did for scopes is make three big nestable flow ops:

if - branch out, no args
loop - reentrant goto, repeat args
label - deferred forward goto, merge args

floooh,
@floooh@mastodon.gamedev.place avatar

@lritter @TomF @pervognsen WASM also only has structured control flow, but to my surprise my hilarious CPU emulator switch-case-goto contraption performs quite well in WASM compared to native (trigger warning for "goto considered harmful" believers):

https://github.com/floooh/chips/blob/bd1ecff58337574bb46eba5e7e16937899360e56/chips/z80.h#L4706-L4767

StompyRobot,
@StompyRobot@mastodon.gamedev.place avatar

@pervognsen @TomF

In a function that sets up more than one thing, if you want to unwind in the error handler without conditionals, you need one go-to per unwind point.

pervognsen,
@pervognsen@mastodon.social avatar

@StompyRobot This is not really true example of goto either. But you're right that this can only be expressed via goto in some languages if you don't want to use conditionals. Rust does not have goto but you can express the pattern you mentioned with blocks and labeled break. Similarly, Wasm does not have goto (and in fact cannot express irreducible CFGs directly) but you can express that clean-up pattern by branching to an outer block.

TomF,
@TomF@mastodon.gamedev.place avatar

@pervognsen @StompyRobot Ah this is the "no true goto, laddie!" fallacy I have heard so much about.

(in fact, they're all gotos)

pervognsen,
@pervognsen@mastodon.social avatar

@TomF @StompyRobot Tom, I didn't say a bad thing about goto. You have to take your meds and stop harassing me in my own thread after I took you off the reply line several times, intentionally.

sgf,
@sgf@mastodon.xyz avatar

@pervognsen @TomF Am I missing something, or can this specific case be written as

i := w;
while i <> 0 and x[i] = 0 do
i := i - 1;
length := i + 1;

?

(The case where x is entirely 0s returning 1 seems odd to me, but matches the original's behaviour.)

pervognsen,
@pervognsen@mastodon.social avatar

@sgf @TomF Classic Pascal does not have a short-circuiting 'and' operator. :)

sgf,
@sgf@mastodon.xyz avatar

@pervognsen @TomF Aha! All becomes clear! Thanks.

I guess you could use Pascal's nested functions to make a loop termination test that hand-implements the short-circuiting, which I think would be clearer than the sentinel shenanigans, but yeah, "break" still wins.

pervognsen,
@pervognsen@mastodon.social avatar

@sgf @TomF The short-circuiting is a funny example of something we all take for granted now. Even in Turbo Pascal, which was the Pascal that was foundational for my own early experiences, short-circuiting was an option you had to explicitly enable (with the equivalent of a scoped pragma/feature at the level of a source file or block of code) since it was not short-circuiting in classic Pascal. And I still remember that it was usually presented as an "optimization" rather than load-bearing.

regehr,
@regehr@mastodon.social avatar

@pervognsen wait, where did "w" come from?

also this reminds me how deeply I hate "<>"

also this is horrible

pervognsen,
@pervognsen@mastodon.social avatar

@regehr It's a global constant: w for width, x is an array [0..w], keeping in mind that this is inclusive Pascal arrays so it actually has w+1 entries.

regehr,
@regehr@mastodon.social avatar
regehr,
@regehr@mastodon.social avatar

@pervognsen I had sort of forgotten how Pascal used "assignment to the function name" to return something. I always disliked this.

pervognsen, (edited )
@pervognsen@mastodon.social avatar

@regehr That part isn't too objectionable to me. And I like Go's take on this with named (multiple) return values. I'm guessing that came to Go via Robert Griesemer, who went to ETH Zurich and probably was directly influenced by Wirth and his languages. As for having the same name as the function in ALGOL derivatives, well, there's a certain elegance to it compared to having to make up a generic variable name like 'result'. Though I agree it looks weird to modern eyes.

pervognsen, (edited )
@pervognsen@mastodon.social avatar

@regehr How much do you hate Haskell's /=? If you're an ingrained C programmer that one is fun. At least it's defensible on the grounds of visual similarity to a strike-through equality sign in hand-written math. And it has precedent in a language (the first language!) since it's what FORTRAN uses. I've never figured out from what origin or analogy ALGOL's diamond-shaped <> is supposed to have originated. That one must be a sui generis product of late 1950s pipe smoke.

regehr,
@regehr@mastodon.social avatar

@pervognsen I don’t write Haskell but I am totally fine with /=

phairupegiont,
@phairupegiont@mastodon.social avatar

@pervognsen @regehr "what origin or analogy", do you mean, besides the "< or >" meaning it has for numbers?

pervognsen,
@pervognsen@mastodon.social avatar

@phairupegiont @regehr Huh I honestly hadn't even made the connection! I always thought it was an attempt at drawing an ASCII diamond.

fanf,
@fanf@mendeddrum.org avatar

@phairupegiont @pervognsen @regehr ieee 754 has <> for fortran .LG. and ?<> for unordered, less, or greater, ie .NE.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • kavyap
  • thenastyranch
  • tester
  • DreamBathrooms
  • mdbf
  • magazineikmin
  • tacticalgear
  • Youngstown
  • ethstaker
  • osvaldo12
  • slotface
  • everett
  • rosin
  • khanakhh
  • megavids
  • ngwrru68w68
  • Leos
  • modclub
  • cubers
  • cisconetworking
  • Durango
  • InstantRegret
  • GTA5RPClips
  • provamag3
  • normalnudes
  • anitta
  • JUstTest
  • lostlight
  • All magazines