[Haskell-cafe] What is your favourite Haskell "aha" moment?
david.feuer at gmail.com
Wed Jul 11 17:09:55 UTC 2018
Haskell is the only language I know of with typeclass coherence.
On Wed, Jul 11, 2018, 12:25 PM Simon Peyton Jones via Haskell-Cafe <
haskell-cafe at haskell.org> wrote:
> Also, languages are borrowing from each other at a rapid rate these days
> (eg Rust traits are equivalent to type classes) so it's hard to find a
> "killer feature" in Haskell any more
> That’s true, and to be celebrated!
> One thing that stands our for me is the ability to abstract over type
> f :: forall (m :: * -> *) (a :: *). Monad m => a -> m a
> That ability is what has given rise to a stunningly huge collection of
> abstractions: not just Monad, but Functor, Applicative, Traversable,
> Foldable, etc etc etc. Really a lot. It opens up a new way to think
> about the world. But only made possible by that one feature. (Plus type
> classes of course.)
> Do any statically typed languages other than Haskell and Scala do this?
> *From:* Haskell-Cafe <haskell-cafe-bounces at haskell.org> *On Behalf Of *Neil
> *Sent:* 11 July 2018 17:12
> *To:* Haskell Cafe <haskell-cafe at haskell.org>
> *Subject:* Re: [Haskell-cafe] What is your favourite Haskell "aha" moment?
> I came to Haskell from C++, and I was used to the idea of parametric types
> and functions from C++ templates.
> However, what I really liked about Haskell's way of doing these was that,
> due to type inference, not only is there a lot less ceremony involved, but
> code is generic (parametric) *by default*, and thus much more reusable,
> although you still have the option of tightening it up by adding a type
> annotation (eg for performance reasons). For a while, I was writing all my
> C++ code as templates, but this ended up being a pain.
> Also, traditionally C++ has not allowed you to place any constraints on
> template arguments (type parameters) whereas Haskell's type classes are a
> very elegant way of doing it. (C++ now has concepts, but I haven't taken
> the time yet to see how they compare with type classes.)
> I was also used to function overloading, which is somewhat similar to type
> inference, in that the compiler will pick an implementation based on the
> types of a function's arguments. This is similar to Haskell picking an
> implementation from among type class instances. However, what blew me away
> is that Haskell can overload based on *return type* as well as argument
> types. I haven't seen any other production-ready language that can do this.
> A great example of how this is useful is the regex library, where you can
> select from among widely varying styles of regex matching result simply by
> type inference, ie without needing any type annotation.
> There were a *lot* of other things I found amazing, but others have
> covered many of these already. Also, languages are borrowing from each
> other at a rapid rate these days (eg Rust traits are equivalent to type
> classes) so it's hard to find a "killer feature" in Haskell any more
> (except laziness, perhaps). I think it's more the well-balanced combination
> of all the features that makes Haskell so pleasant to work in, and it's
> hard to demonstrate all of these in a single example.
> My own favourite "gem" is this code for computing all primes, based on
> code in a paper by Doug McIlroy:
> primes = sieve [2..] where sieve (p : ns) = p : sieve [n | n <- ns, n
> `mod` p /= 0]
> I think care must be exercised, when using examples like this one, to
> avoid giving the impression that Haskell is a "toy" language. However, what
> I find interesting about this example is that all other sieve
> implementations I've seen work on a fixed size of sieve up front, and if
> you later change your mind about how many primes you want, eg because
> you're expanding a hash table and want a bigger prime for the size, you
> typically have to start the sieve from scratch again.
> : http://www.cs.dartmouth.edu/~doug/sieve/sieve.pdf
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe