12 ms·
"... I can't remember the last time I saw a program with a complicated control flow done by gotos." Perhaps not "complicated" enough, but here's a fizzbuzz in
by 1vuio0pswjnm7 5y ago
"... I can't remember the last time I saw a program with a complicated control flow done by gotos."
Perhaps not "complicated" enough, but here's a fizzbuzz in spitbol (a fast SNOBOL interpreter). This is an unstructured, goto based language for non-numeric computation developed at Bell Labs. Would love to see a faster version in some popular, "structured" scripting language in the same number of characters. I find I can write scripts in more aesthetically pleasing ways than with "structured" languages.
But of course aesthetics is subjective. (The term "subjective" here means what looks good to you might not look good to me, and vice versa.)
;var a = 0
;start a = a + 1
;break lt(a,101) :f(end)
;x01 y = remdr(a,3)
;x02 z = remdr(a,5)
;x03 x = y + z
;x01a eq(x,0) :s(x06)
;x02a eq(y,0) :s(x04)
;x03a eq(z,0) :s(x05)f(x07)
;x04 output = 'fizz' :(start)
;x05 output = 'buzz' :(start)
;x06 output = 'fizzbuzz' :(start)
;x07 output = a :(start)
;end
Perhaps the unintended benefit of gotos is that it becomes foolish to try to construct overly complex control flow. Whereas stuctured programmming seems to encourage control flow complexity. The use of the phrase "complicated control flow" in the parent comment is a great example. It suggests for the author structured programming allows and arguably therefore encourages such complexity. But what benefit is served by creating "complicated control flow". Why not aim for simpler control flow.
- mycodesucks 5y agoThis is the opposite of aesthetically pleasing to my eye.
- thaumasiotes 5y agoAesthetically, it bothers me that you compute x by calling remdr two extra times, as opposed to computing it as the sum of y and z... or the bitwise OR of y and z... What's the difference between :(label) / :s(label) / :f(label) / :s(label1)f(label2) ?
- 1vuio0pswjnm7 5y agos and f are different conditions s=success f=failure default is unconditional Is bitwise OR a built-in or would it require writing a separate function.
- thaumasiotes 5y agoBased on the documentation, there do not appear to be any bitwise operations available in SNOBOL. It looks like computing x as remdr(3) + remdr(5) is a pure mistake. You only care about the case where remdr(15) is 0, and it's true that remdr(3) + remdr(5) is 0 if and only if remdr(15) is 0. But you're calling remdr twice where you should only be calling it once. (Or, really, you can keep the weird calculation of remdr(3) + remdr(5), which is conceptually incorrect but correct in every case that matters, but stop computing it with two extra calls to remdr. You're already calculating those values and storing them in the variables y and z; compute x as y + z, which must be much faster than calling remdr two additional times.) As a side note, why do you want to use "a language for non-numeric computation" for this purely numeric problem?
- 1vuio0pswjnm7 5y agoI think the term "non-numeric" probably refers to another popular language of the era from which SNOBOL comes called FORTRAN. The purpose of fizzbuzz as I understand it is to test a person's aptitude for programming, but not necessarily a person's ability to select the "right" programming language. If the purpose of fizbuzz is to test for programming language selection, and fizzbuzz is a "purely numeric problem", then should the proper choice be FORTRAN.
- masklinn 5y ago> What's the difference between :(label) / :s(label) / :f(label) / :s(label1)f(label2) ? Looks like it’s unconditional, conditional (success), and conditional (failure) jumps. :s(A)f(B) is how you tell it to jump to A on success and B on failure.
- 1vuio0pswjnm7 5y agoThere is also an "indirect goto" where B is a variable to be appended to string A to compute a label that is defined somewhere else in the program. :($('A' B))
- wtallis 5y ago> But of course aesthetics is subjective. (The term "subjective" here means what looks good to you might not look good to me, and vice versa.) Bullshit. There's so much that objectively deficient about that piece of code that I'm having trouble taking you seriously. If that comes at all close to being a representative sample of someone trying to make clear, readable code in that language, then the language was clearly deliberately designed to have an excess of visual noise. But I suspect you're not actually trying to make that code look as clean as the language allows. The semicolons serve no apparent purpose. Having labels like x01 and x01a with no functional relationship between those lines is outright hostile to the reader. Using x as the prefix for almost all of your label names and as one of your variables is mildly confusing, especially when most of those lines don't relate to the variable x and don't appear to need labels at all. "output" seems to be a magic variable for which assignment has side effects, but nothing in the syntax differentiates it from any other variable. Grouping all the conditionals together, then grouping all their consequences together forces the reader to jump back and forth between lines more than if you put the consequence of each conditional immediately after the corresponding test (in this instance, your way does have the advantage of making the conditional fall-through pattern more readily apparent, but on the other hand that wouldn't be as necessary if you could use more than one line of code to write a single line of output). You can claim all you want that aesthetics is subjective, but at the very least you must recognize that visual noise you've trained yourself to ignore is objectively different from code which doesn't have those distractions in the first place.
- 1vuio0pswjnm7 5y ago"output" is a keyword, cannot change it (I guess one could recompile the interpreter and define it as something else) semicolons separate statements. Dont like them at the beginning, move them to the end. Dont like the label names. Rename them to whatever suits you. The thing that you may be misunderstanding is that this is written for me, not you. It is written to please my sense of aesthetics, not yours. I am not employed to write programs for other users or to be read by programmers. Every user is different. You probably could not write a program I would use. I would find "deficiencies" and reject it for something else. If programming is objective then why are there thousands of different languages, and the number is constantly increasing. All of them are basically doing the same things. Honest question. Why are there so many different languages. Whats behind that.
- regularfry 5y ago"Structured programming" has a very specific meaning. Or at least, it did in Dijkstra's goto paper. It's not that all uses of goto are terrible, it's that if you restrict goto's uses to specific types of control flow, you can make provable statements about the behaviour of the code, which other uses blow up. The goto's you've used here are within the set of allowable uses, so can be mapped directly onto structured constructs. There's literally no advantage either way, other than introducing the possibility to the reader that there could be something weird going on in your code. > Would love to see a faster version in some popular, "structured" scripting language in the same number of characters. How's this? #include <stdio.h> void main(){ for(int a=1; a<100; a++){ int y = a % 3; int z = a % 5; int x = y + z; if(!x) puts("fizz"); else if(!y) puts("buzz"); else if(!z) puts("fizzbuzz"); else printf("%d\n",a); } } Same technique, give or take some unimportant details. Not particularly golfed. 240 characters to your 312. I have no idea if it's faster because I don't know how good the spitbol interpreter is, but I do know that because I've used "structured programming" structures, the compiler (or interpreter, whichever you prefer) can in principle make more safe optimisations to my code than to yours. If C doesn't meet the "scripting language" constraint (which I find arbitrary, but not insurmountable), I'd throw Julia or FORTH at it and probably shave a considerable number of bytes off.
- 1vuio0pswjnm7 5y ago"How's this?" Unoptimised, it's already roughly 3x faster than the spitbol script. Moving the "int" specifier out of the for loop allows this to compile under -std=C89. How does one suppress the warning about the non-int return value for main().
- regularfry 5y agoThe easiest way is to `return 0;`. We've got the character budget for it.
- antonvs 5y agoI really don't understand the comparison you're trying to make. I learned SNOBOL from a university library book in the early 1980s, and used it on the university mainframe - but it's baffling to me that someone might want to continue using it today for anything other than nostalgic reasons. (If you're trolling, good one!) There's an enormous amount of accidental/inessential complexity in the code you provided, and that's a big part of the reason no-one seriously uses SNOBOL or similarly primitive languages for anything serious today. Code like the above can't remotely scale to larger programs, and attempts to do so were notorious for their failure. Here's a version of FizzBuzz in e.g. Haskell: fizz n | n `mod` 15 == 0 = "FizzBuzz" | n `mod` 3 == 0 = "Fizz" | n `mod` 5 == 0 = "Buzz" | otherwise = show n mapM_ putStrLn $ map fizz [1..100] Even the already rather simple control flow is abstracted away by the 'map' function, so there are no visible loops or gotos. Compare that to your program with a dozen labels and seven gotos, where the actual logic is buried in irrelevant cruft. Even versions with more explicit control flow (i.e. without map) can be similarly readable, but if your goal is to minimize complex control flow, then rather than reverting to intractable gotos, go read "Lambda: The Ultimate GOTO" (http://dspace.mit.edu/handle/1721.1/5753 http://dspace.mit.edu/handle/1721.1/5753), published in 1977, and learn to use functions for control flow.
- 1vuio0pswjnm7 5y agoBetter than Haskell and faster, too: fizzbuzz:{v:1+!x;i:(&0=)'v!/:3 5 15;r:@[v;i 0;{"Fizz"}];r:@[r;i 1;{"Buzz"}];@[r;i 2;{"FizzBuzz"}]};`0:$fizzbuzz 100
- antonvs 5y agoYou're wasting your own time.
- deleted 5y ago[deleted]