{"id":225,"date":"2006-11-26T15:16:57","date_gmt":"2006-11-26T15:16:57","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2006\/11\/26\/why-haskell\/"},"modified":"2006-11-26T15:16:57","modified_gmt":"2006-11-26T15:16:57","slug":"why-haskell","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2006\/11\/26\/why-haskell\/","title":{"rendered":"Why Haskell?"},"content":{"rendered":"<p>Before diving in and starting to explain Haskell, I thought it would be good to take a moment and answer the most important question before we start:<\/p>\n<p>**Why should you want to learn Haskell?**<\/p>\n<p>It&#8217;s always surprised me how many people *don&#8217;t* ask questions like that.<br \/>\nHaskell is a valuable language for a lot of different reasons, but the most important one is that *it<br \/>\nchanges the way that you think about programming*. Haskell does things in a very different way from the imperative languages that most of us are familiar with. And it&#8217;s an extremely well-designed language, so there isn&#8217;t a ton of crap standing between you and the concepts that you can learn from it.<\/p>\n<p><!--more--><br \/>\nBefore I get into what&#8217;s so interesting about Haskell, I want to get one thing out of the way. One of the most commonly cited reasons for why Haskell is good is, in my opinion, silly.<br \/>\nThe number one thing that you&#8217;ll often hear from functional programming afficionados is that Haskell<br \/>\nis *referentially transparent*, which makes it easy to do things like reason about programs, and write correctness proofs of programs. Writing a correctness proof of a C program (or even an Objective CaML program) ranges from difficult to effectively impossible &#8211; the presence of side effects in the code is *very* hard to cope with in formal reasoning, and when programs get beyond trivial simplicity, they rapidly become intractable for proofs.<br \/>\nReferential transparency is a good thing. But the reason why it&#8217;s good has very little to do with<br \/>\ncorrectness proofs. The whole &#8220;correctness proof&#8221; line is silly because *no one writes correctness proofs*. Real programs &#8211; programs that solve real problems &#8211; are generally large and complex enough that even if you&#8217;re using a language like Haskell, writing a correctness proof *still* isn&#8217;t practical. Sure, you can easily write a correctness proof for an implementation of a binary search tree in Haskell. You *can* write a correctness proof for that in, say, Java. But the Haskell proof will be easier and cleaner, and you&#8217;ll be able to trust the connection between the code and the proof much more than you could in Java. But really, a good set of tests &#8211; which you *should* be writing, whether you&#8217;re programming in Java, C, or Haskell &#8211; does as good a job of verifying the correctness of something simple like that. And for anything significantly more complicated than that, people just *don&#8217;t* write proofs. I&#8217;ve certainly *never* seen a correctness proof for a Haskell program, except in papers arguing about how provable correctness is in Haskell. (Does GHC have correctness proofs? I doubt it. It&#8217;s certainly had quite its share of bugs, which means that either it&#8217;s correctness isn&#8217;t proven, or the proof is wrong!)<br \/>\nSo what makes Haskell so wonderful? Or, to ask the question in a slightly better way: what is so great about the pure functional programming model as exemplified by Haskell?<br \/>\nThe answer is simple: *glue*.<br \/>\nLanguages like Haskell have absolutely *amazing* support for modular development. You build the basic semantics of your system in terms of primitive functions, and then the language provides tons of very elegant *glue* for putting the pieces together. In an imperative language, the fact that the order of execution is highly constrained by side effects means that the ways of connecting components are limited by the fact that you could break things if the glue doesn&#8217;t work quite right.<br \/>\nBut the basic properties of Haskell make it much easier to build generic glue. Lazy evaluation<br \/>\ncombined with no side effects means that many of the problems that make generic glue difficult in<br \/>\nimperative languages just simple don&#8217;t exist in Haskell. You don&#8217;t need to worry about whether the<br \/>\niteration glue will correctly ensure that array element *n* is updated before array element *n+1*; lazy evaluation will take care of making sure that the data dependency order is respected. And the fact that building new functions, and currying existing ones is a lightweight, syntactically trivial operation means that the glue can be written clearly and concisely.<br \/>\nThe Monad concept, which I&#8217;ll get to in a later post, is an amazing example of this. Monads are a basic tool for *sequencing*: when things need to be done in sequence, the Monad constructs allow you to make that happen. But there&#8217;s not just *one* monad that does sequencing. There&#8217;s a basic *concept* of what a monad is, and *anything* you implement that fits that concept can use *all* of the monad tools provided by a language. So I can use the same constructs for writing sequencing code for list processing, for performing IO, for writing numeric code that does updates to mutable arrays, for combining non-deterministic computations, and more.<br \/>\nA very typical example of that elegance of glue in Haskell is this implementation of quicksort:<br \/>\nqsort []     = []<br \/>\nqsort (x:xs) = qsort smaller ++ [x] ++ qsort bigger<br \/>\nwhere smaller = filter (=x) xs<br \/>\nYou can probably understand that code even *without* knowing anything about Haskell. What it says is:<br \/>\n1. The result of sorting an empty list is an empty list.<br \/>\n2. To sort anything bigger than an empty list, do a divide-and-conquer:<br \/>\n1. Get the first element of the list, `x`, and use it to divide the list into two sublists: things smaller than `x`, and things bigger than `x`.<br \/>\n2. The sorted list is the result of concatenating the results of sorting the list of smaller elements, `x`, and the results of sorting the list of bigger elements.<br \/>\n3. To get the list of smaller elements, select the list of elements from the list that are less than `x`; and to get the list of bigger elements, select the elements from the list that are greater than or equal to `x`.<br \/>\nThis uses a generic concatenation function (&#8220;`++`&#8221;), a generic filter function for selecting the sublists (&#8220;`filter`&#8221;), and a simple currying operation to generate the comparison functions for using filter to create the sublists. (&#8220;`(&lt;x)`&quot; is Haskell notation for the currying of &quot;`&lt;`&quot;; it&#039;s equivalent to &quot;((&lambda; x . (&lambda; y . y &lt; x) x)&quot; in lambda calculus.  For the purposes of what I&#039;ve been discussing, the concatenation and filter operations are typical Haskell glue, and the<br \/>\n&quot;`where`&quot; clause is a typical Haskell idiom for splitting things up to make the glue<br \/>\neasier to read.<br \/>\nThere&#039;s a lot more about Haskell that&#039;s great like the strong type system, the Monad constructs,<br \/>\nuser definable operators, type classes, and more. But in my own humble opinion, the thing that makes it all really come together is the way that the language makes it so easy to build big complex things by gluing together small simple pieces.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before diving in and starting to explain Haskell, I thought it would be good to take a moment and answer the most important question before we start: **Why should you want to learn Haskell?** It&#8217;s always surprised me how many people *don&#8217;t* ask questions like that. Haskell is a valuable language for a lot of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[89],"tags":[],"class_list":["post-225","post","type-post","status-publish","format-standard","hentry","category-haskell"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-3D","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/comments?post=225"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/225\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=225"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}