22
Worldyn
7y

Just started learning haskell and if someone wants a 'why' I give you quicksort:

quicksort [] = []
quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)
where
lesser = filter (< p) xs
greater = filter (>= p) xs

Comments
  • 5
    @LeFlawk It's a good example of the difference between declarative and imperative programming. Imperative: change states and then get a final result. Declarative: "this is what this is". The parameter (x:xs) is a list [] where x is the first element and xs the rest of the list. You take x and say "everything greater is on the right side" and "everything lesser vice versa". So you DECLARE how it is recursively instead of changing what is inside the list recursively. The ++ operator just adds lists together
  • 0
    @LeFlawk when I said x I meant p
  • 2
    That's a stupid example to show the why.
    From all the things that Haskell has to offer, Monads, ADTs, Typeclasses, ... you show a quicksort -.-
  • 0
    Reminds me of prolog!
  • 2
    @disolved Really?
    I could understand when someone says that about dynamically typed, prologish pattern matching supporting Erlang, but Haskell?
  • 1
    @ac1235 It was a purely subjective observation, based on Prolog being my frame of reference for declarative languages. Was it really that hard to believe?
  • 1
    Just. Beautiful.
  • 1
    @ac1235 Good example of declarative and functional programming style. Everything else is awesome too but you gotta start selling from somewhere ;)
  • 0
  • 0
    @disolved So K reminds you of Prolog, because it is declarative?
  • 0
    @ac1235 and my learning is a work in progress so haven't gotten to know everything yet 👍
  • 2
    @ac1235 if i wrote quicksort in prolog it would look and feel very similar to the example. In k it would not.
Add Comment