TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Loop Invariants

47 pointsby kerckerabout 9 years ago

7 comments

justinsaccountabout 9 years ago
&gt; If the current comparision shows a smaller value, we update smallestSoFar.<p><pre><code> if ( a[smallestSoFar] &gt; a[nextToCheck] ) { </code></pre> This code makes me twitch. Finding a smaller value by checking to see if the old value is larger than the new value... Instead of checking to see if the new value is smaller than the old value.<p>Additionally, calling the value you are <i>currently</i> checking &#x27;nextToCheck&#x27; is also confusing. It&#x27;s not the next one, it&#x27;s the current one.
评论 #11196553 未加载
PaulHouleabout 9 years ago
One of the things I find funny is that the halting problem is the least of my concerns working in mainstream programming languages today.<p>Maybe back in the day when people were using basic and assembly language it was easy to write some gawdawful mess but since structured programming I think it is easier to do the right thing rather than the wrong thing. Partially because we have good constructs to work with, secondly they are good constructs to reason with -- for instance, you might have the idea of a loop invariant in your head when you write the loop, even if you don&#x27;t call it that.
评论 #11196575 未加载
评论 #11196137 未加载
tmerrabout 9 years ago
Hey, this seems like an application of induction. You choose some property you want to prove about your program, show it is true before the loop (base case), then show it remains true for any next iteration (inductive step), therefore it is true for all iterations.<p>It reminds me of the hand-wavy inductive proofs I run through my head when I write recursive functions. Usually it&#x27;s not even about eliminating bugs, it&#x27;s just the most natural way to understand why the code works. It seems less natural for imperative for&#x2F;while loops but still a good tool to keep in mind.
评论 #11204597 未加载
评论 #11196644 未加载
评论 #11197057 未加载
yodsanklaiabout 9 years ago
As a source of motivation for students, does anyone could point me to some &quot;real (open source) code&quot; where the programmer commented a loop with an invariant for clarity.
评论 #11196124 未加载
评论 #11196318 未加载
评论 #11196624 未加载
mhdabout 9 years ago
This reminds me of Eiffel, where loop (in)variants had to be explicitly declared, as part of its design-by-contract dogma.
评论 #11195776 未加载
Rhapsoabout 9 years ago
If I made a zeno&#x27;s paradox loop:<p><pre><code> bigFloat delta = 0.5 bigFloat total = 0.0 while total &lt; 1.0: total+=delta delta*=0.5 </code></pre> Where does this fit in the model of using a loop invariant to show the loop terminates? The loop is always approaching the terminal case (and eventually the machine runs out of memory)
评论 #11195933 未加载
评论 #11195974 未加载
评论 #11196762 未加载
评论 #11198742 未加载
评论 #11195883 未加载
评论 #11197004 未加载
mrestkoabout 9 years ago
A few more examples would have been nice.