4 ms·
I definitely agree with the documentation side of things, particularly lack of concrete examples. I found that overall however, learning Haskell made me a bett
by madog 6y ago
I definitely agree with the documentation side of things, particularly lack of concrete examples.
I found that overall however, learning Haskell made me a better programmer, even if it's more useful as more of an academic than practical language.
Dealing with pure functions, no access to loops so recursion is paramount and it is rather beautiful and useful some of the tail recursion and pattern matching stuff.
The Haskell quicksort is the classic example of this:
quicksort [] = []
quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)
where
lesser = filter (< p) xs
greater = filter (>= p) xs
- deleted 6y ago[deleted]
- leereeves 6y agoThat is quite elegant for that variant, which is a great fit for Haskell's features. How easy is it to write a different variant, like using a different pivot, or sorting in place?
- seppel 6y ago> The Haskell quicksort is the classic example of this: The Haskell quicksort is also classic example of something that is small, beautiful and still misses the point. Yes, it contains the core idea of quicksort (partion the list and divide and conquer) but it completely fails on the quick part, because the Haskell lists are are leaky abstraction of real computer memory. A real quicksort in Haskell is much more convoluted.
- hashingroll 6y agoWhat's a real quicksort anyway? You could argue that a reasonably fast implementation of quicksort is much more convoluted, which it most certainly is, but that doesn't make this implementation any less real.
- seppel 6y ago> What's a real quicksort anyway? A key aspect of quicksort is that it sorts the list in-place. If you dont sort in-place, you dont have quicksort and if you dont need in-place sort, then quicksort is the wrong choice anyway. Of course, canonical Haskell does not have a concept of in-place, which makes showing quicksort in Haskell also a questionable idea. > You could argue that a reasonably fast implementation of quicksort is much more convoluted, which it most certainly is, but that doesn't make this implementation any less real. A reasonably fast implementation of quicksort is straight-forward in any language that has arrays/vectors with destructive updates. This implementation will have issues with pathological cases, but that's a problem of the quicksort algorithm, not of the implementation (whereas the Haskell one shown above has problems in the implementation).
- n4r9 6y agoSounds like a hardware issue.
- oblio 6y agoWhich hardware does not have this issue?
- gspr 6y ago> I found that overall however, learning Haskell made me a better programmer, I definitely had that experience, too!
- mcintyre1994 6y agoIt's worth noting though that in-place quicksort really doesn't play to Haskell's strengths - there are some examples of the code for it here: https://stackoverflow.com/questions/7717691/why-is-the-minimalist-example-haskell-quicksort-not-a-true-quicksort https://stackoverflow.com/questions/7717691/why-is-the-minim...
- Blikkentrekker 6y agoOne cann do this in Python, or any other language for that matter: def quicksort ( xs ): if len(list) == 0: return [] else: less = [x for x in xs[1:] if x <= xs[0]] more = [x for x in xs[1:] if x > xs[0]] return less + [xs[0]] + more Not the most efficient implementation in either.
- wheybags 6y agoAnd IMO, that python version is one million % more readable. In particular, because you defined more and less before using them, vs after in the Haskell example. I don't know if there's a way that could be achieved in Haskell too though. Edit: you forgot the recursive calls though.
- harporoeder 6y agoHaskell has a `let ... in` syntax. Quoting from (1): quicksort1 :: (Ord a) => [a] -> [a] quicksort1 [] = [] quicksort1 (x:xs) = let smallerSorted = quicksort1 [a | a <- xs, a <= x] biggerSorted = quicksort1 [a | a <- xs, a > x] in smallerSorted ++ [x] ++ biggerSorted 1: http://learnyouahaskell.com/recursion http://learnyouahaskell.com/recursion
- FridgeSeal 6y agoI used to think so as well, until I realised at some point that defining things like this means you focus on the actual "business logic" up front, but the applicable definitions are never far (visually, spatially and logically). In my opinion it lets you get to grips on the overall logic before hassling you with certain specifics.
- adsharma 6y agoThe term "business logic" itself tends to be somewhat controversial: https://adsharma.github.io/flattools-programs/ https://adsharma.github.io/flattools-programs/
- 414owen 6y ago
- lawn 6y ago> I found that overall however, learning Haskell made me a better programmer I had the same experience, but with ML. It almost feels like Haskell with the annoying parts removed.
- useerup 6y agoQuicksort in one line: Quicksort = { []->[], [p,,m]->this(m??<p) + (m??==p) + this(m??>p) }