5 ms·
> 0) Are there problems for which writing a WHILE loop is the way to go even though they are not needed? Yes. Often one can (after much thought and code) writ
by 082349872349872 3y ago
> 0) Are there problems for which writing a WHILE loop is the way to go even though they are not needed?
Yes. Often one can (after much thought and code) write something that explicitly reduces a problem in just* the right order, but it is quicker and simpler to repeatedly apply a set of rewrites WHILE any changes have been made.
* see Knuth on Tarjan's SCC
- zmgsabst 3y agoEven nested while loops: while not stream.closed: symbols = [] current_symbol = None while not current_symbol == END: current_symbol = stream.read() symbols.append(current_symbol) # Do stuff with symbols. I’d argue that “while” is natural any time you have co-inductive types, and want to traverse until deconstruction stops.
- magicalhippo 3y agoI also find "while" natural whenever I have some precondition. For example if I need to take a lock before checking for the terminating condition while holding the lock.
- valenterry 3y agoSorry, but it's not natural for me at all. In fact, when I learned programming, that was one of the harder things to wrap my head around. In English, when you say while A, B" it means "as long as condition A is fulfilled, condition B is also fulfilled". Like "while the window is open, the room is getting colder". But then in programming, that's not how it works. "while the stream is not closed..." what? A variable called symbols is being assigned. That's an action - so how often does it happen? Once or multiple times? Can the code after the while run in parallel or only sequential? And don't even get me started with forgetting to close the stream. Etc. etc. Intuitively it would be: "1.) open a stream. 2.) If there is nothing more to read, close the stream, otherwise read one symbol and add it to the list of symbols. Repeat 3.) repeat step 2". So for me at least, the recursive version is much more intuitive. it's how I think about it and how I would think about it if it were a real-world problem too.
- Scarblac 3y agoIt just means "while A, do B". The do is implicit, just like with if, for, etc.
- xnorswap 3y agoThe Do is explicit in some languages like Visual Basic: https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/do-loop-statement https://learn.microsoft.com/en-us/dotnet/visual-basic/langua... Now it's a bit confusing if you're used to other languages, because you might think "while" is a test-at-start and "Until" is test-at-end, but actually while and until can both be tested either at the start or the end, and While and Until are just testing the negation of each other. It can also be extra confusing, because there is a separate While.. End loop without the Do that can be used instead. There's a reason Visual Basic died out, and it isn't just the snobbishness of seeing C# as more of a "real" language.
- taeric 3y agoI confess various Basics all dying out feels more and more like the result of snobbishness as I get older.
- saghm 3y ago> In English, when you say while A, B" it means "as long as condition A is fulfilled, condition B is also fulfilled". Like "while the window is open, the room is getting colder". > But then in programming, that's not how it works. "while the stream is not closed..." what? A variable called symbols is being assigned. That's an action - so how often does it happen? Once or multiple times? Can the code be after the while run in parallel or only sequential? And don't even get me started with forgetting to close the stream Ruby has the "until" keyword as an alternative to "while not" (similar to but not as commonly needed as "unless" for "if not"). I've honestly always kind of liked Ruby's willingness to go out of its way to try to make things like this more natural, but sadly I think I'm in the minority on this one. The one that always feels the most funky to me is whenever I have to invert `is_empty` in some way; I wish that languages would standardize having a method that means the opposite purely for aesthetic reasons, which I suspect would be wildly unpopular with everyone else besides me due to being seen as "redundant".
- aragonite 3y agoLots of mundane human activities are most naturally represented as while (or do-while) loops. For example, wiping one's butt: do { wipe() } while { color_of_tp() !== "white" } Or anything that falls under cleaning, really. Although I'd argue while loops belong most at home in imperative code. (Cleaning for example is like the impure function par excellence, done only for side effect)
- ape4 3y agoI think this code would be nicer if the wipe() function returned the color ;)
- deleted 3y ago[deleted]
- dmichulke 3y agoYou should not use "white" but the initial color_of_tp()
- Vvector 3y agoAnd brown TP causes your program to be stuck in endless loop
- misja111 3y agoI would definitely stop wiping if the color turned to red.
- culi 3y agoi recommend witch hazel
- pixel8account 3y agoIronically this one may actually be better represented by a for loop, because after 20 times or so you might want to reconsider your situation.
- tzs 3y ago