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.

Trying Haskell

37 pointsby chegra84about 14 years ago

4 comments

Peakerabout 14 years ago
Haskell's type system can also express interesting things most languages' type systems can't.<p>For example:<p><a href="https://github.com/yairchu/red-black-tree/blob/master/AvlTree.hs" rel="nofollow">https://github.com/yairchu/red-black-tree/blob/master/AvlTre...</a> -- in lines 13..21, an AVLTree type is defined -- with its invariants encoded in the type system. If there's a mistake in these 9 lines, you may get a wrong program. But the nice thing is that if you get just these 9 lines right -- then the hundreds of lines below it that implement an AVL tree <i></i>cannot get the AVL invariants wrong<i></i>.<p>The same is also true for Red Black Trees: <a href="https://github.com/yairchu/red-black-tree/blob/master/RedBlackTree.hs" rel="nofollow">https://github.com/yairchu/red-black-tree/blob/master/RedBla...</a><p>Lines 26..36 inclusive encode the RBTree type such that the invariants are enforced by the type-checker.<p>In case this is unclear: the type-checker enforcing the correctness of the invariants is at <i>compile-time</i>. A running program is a correct program, at least from the invariants' perspective.
评论 #2430437 未加载
评论 #2430387 未加载
评论 #2429818 未加载
评论 #2429758 未加载
Tiomaidhabout 14 years ago
"Oh look, Haskell is great! Why? Static typing and concise code and no side effects!"<p>I don't disagree on any particular point, but this is hardly news (on HN, at least), and is rather lacking in substance.
评论 #2429771 未加载
ionfishabout 14 years ago
Obligatory comment pointing out that the fabled "quicksort" example in Haskell is not actually quicksort proper since it's not in-place.
评论 #2429904 未加载
评论 #2429582 未加载
评论 #2429443 未加载
评论 #2429632 未加载
pdhborgesabout 14 years ago
C++ quicksort :|<p><pre><code> #include &#60;algorithm&#62; #include &#60;iterator&#62; #include &#60;functional&#62; using namespace std; template &#60;typename T&#62; void sort(T begin, T end) { if (begin != end) { T middle = partition (begin, end, bind2nd(less&#60;iterator_traits&#60;T&#62;::value_type&#62;(), *begin)); sort (begin, middle); sort (max(begin + 1, middle), end); } } </code></pre> from wikipedia
评论 #2430255 未加载