3 ms·
Sorry, 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
by valenterry 3y ago
Sorry, 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".
- karatinversion 3y agoI always write a negative “is empty” check as “size > 0” for this reason