News for hackers. Stuff that matters.
Re:What I Wish I Knew When Learning Haskell by tmountain on Saturday April 11, 2020 @09:02AM (#22841458)
I disagree.

  quicksort [] = []  
  quicksort (x:xs) =   
      let smallerSorted = quicksort [a | a <- xs, a <= x]  
          biggerSorted = quicksort [a | a <- xs, a > x]  
      in  smallerSorted ++ [x] ++ biggerSorted
Re:What I Wish I Knew When Learning Haskell by edflsafoiewq on Saturday April 11, 2020 @09:33AM (#22841581)
That is (infamously) not quicksort.
Re:What I Wish I Knew When Learning Haskell by remcob on Saturday April 11, 2020 @11:24AM (#22842131)
What's wrong with it?
Re:What I Wish I Knew When Learning Haskell by tossup8536 on Saturday April 11, 2020 @01:02PM (#22842665)
Performance. Internal representation. You are not actually modifying things in place. Those are the equivalent of linked lists not arrays. That quicksort is not quick at all because the append operation in Haskell is linear. You can't use those lists to implement quicksort.
Re:What I Wish I Knew When Learning Haskell by lonelappde on Saturday April 11, 2020 @02:25PM (#22843194)
Haskell is lazy. a ++ b === (head a) : ((tail a) ++ b) (plus the base case)

It is only linear when you force it to evaluate, which ideally happens only once (amortized) in your program when the result is consumed. As long as you are careful, you don't get the bad quadratic performance that you'd get from something like eagerly appending a character to a string in a loop in Java. There are either gotchas, though, including relying on the compiler to do things in constant space that naively look linear, and also knowing when the compiler won't help you and you have to structure your computation manually.

Re:What I Wish I Knew When Learning Haskell by Koshkin on Saturday April 11, 2020 @03:25PM (#22843561)
“Modifying things in place” is, from the logical standpoint, an unnatural and even dangerous thing. A person that is not familiar with (imperative) programming often has a hard time understanding variables and assignments (similar to some programmers not understanding pointers). Mathematics doesn’t have assignments, either. That should tell you something...
Re:What I Wish I Knew When Learning Haskell by vkou on Sunday April 12, 2020 @12:43AM (#22846112)
It mostly tells me that mathematics does not care about runtime or space performance. Which is fine, because mathematics is about solving abstract problems, not getting a Turing machine to solve concrete problems.
Re:What I Wish I Knew When Learning Haskell by recursive on Saturday April 11, 2020 @04:08PM (#22843846)
In that case, quicksort requires danger and artifice. In any case, it's not quicksort.
Re:What I Wish I Knew When Learning Haskell by CyberDildonics on Saturday April 11, 2020 @10:22AM (#22841820)
Won't this have to iterate through linked lists and create multiple new heap allocations on each recursive step? Doesn't that imply n log n heap allocations just to sort a list?
Re:What I Wish I Knew When Learning Haskell by remcob on Saturday April 11, 2020 @11:29AM (#22842165)
Yes, but due to lazy-evaluation that won't happen until you do something with the sorted list. Your performance may just blow up in some seemingly unrelated place.

My biggest problem with Haskell is how GC and lazy-evaluation makes it very difficult to reason about what the hardware is actually doing at a given point. I know there ways to inspect and control it, but I've found myself preferring languages that have simpler mental models.

Re:What I Wish I Knew When Learning Haskell by CyberDildonics on Monday April 13, 2020 @10:46PM (#22862281)
Right, by the time you have to control it, you are washing away all the advantages for using it.
Re:What I Wish I Knew When Learning Haskell by magicalhippo on Saturday April 11, 2020 @09:16AM (#22841508)
Not that I know any Haskell, but wouldn't smallerSorted already include x due to <= operator?

edit: also, how is the pivot element selected there?

Re:What I Wish I Knew When Learning Haskell by zests on Saturday April 11, 2020 @09:41AM (#22841610)
Both of your questions have the same answer. (x:xs) means x is the first element and xs is the rest.
Re:What I Wish I Knew When Learning Haskell by magicalhippo on Saturday April 11, 2020 @11:01AM (#22841998)
Aha! Thanks.

Obviously picking first element as pivot makes for a very poor Quicksort. Though I presume a better approach, say median of first, mid and last elements, would add but an extra line?

Re:What I Wish I Knew When Learning Haskell by sullyj3 on Sunday April 12, 2020 @10:31AM (#22848475)
The reason the first element is used as the pivot is that these are linked lists, not arrays, so it would take linear time to access the middle and end. This isn't a real quicksort.