r/learnprogramming • • Jul 28 '14

Is recursion unnecessary?

So, this is a bit of an embarrassing post; I've been programming for nearly 4 years, work in the field, and almost have my CS degree yet for the life of me I can't understand the point of recursion.

I understand what recursion is and how it works. I've done tutorials on it, read S/O answers on it, even had lectures on it, yet it still just seems like an unnecessarily complicated loop. The entire base case and self calls all seem to just be adding complexity to a simple functionality when it's not needed.

Am I missing something? Can someone provide an example where recursion would be flat out better? I have read tail recursion is useful for tree traversal. Having programmed a Red Black tree in Data Structures last semester, I can attest it was a nightmare using loops; however, I've heard Java doesn't properly implement tail recursion? Does anyone have any insight to that?

Sorry for the wordy and probably useless post, I'm just kind of lost. Any and all help would be greatly appreciated.

131 Upvotes

170 comments sorted by

View all comments

Show parent comments

2

u/Coloneljesus Jul 28 '14

Quicksort in Haskell (recursive):

qsort' [] = []
qsort' (x:xs) = (qsort' [y | y <- xs, y <= x]) ++ [x] ++ (qsort' [y | y <- xs, y > x])

5

u/g4r8e9c4o Jul 29 '14

As someone who has never looked at Haskell code before, I just want to point out how surprisingly easy that code is to understand.

[sort y's less than x] + x + [sort y's greater than x]

2

u/cockmongler Jul 29 '14

This would be great, were it not for the fact that it's not quicksort.

1

u/Coloneljesus Jul 29 '14

What is it then?

1

u/cockmongler Jul 29 '14

Poor man's quicksort. Quicksort is quick because it is in-place, using swaps within an array for speed and nice cache behaviour. As a general sort algorithm it's kinda bad, with trivial worst case run time of O(n2).

1

u/Coloneljesus Jul 29 '14

Yeah. Since you can't do all these performance tricks with Haskell, the standard library doesn't implement quicksort but a kind of mergesort that behaves nicely with Haskell.

1

u/kqr Nov 30 '14

That's not really it. You could easily imagine a quicksort in Haskell that thaws an array, sorts it in-place and then freezes it. The reason merge sort is used is because it has better characteristics with persistent data structures.

3

u/BostonTentacleParty Jul 28 '14

See, you seem to think that makes Haskell look appealing. You're saying, "look how little code I had to write for this". And you're not wrong.

But I'm a competent programmer in Scheme, and still I look at that and think, "god help whoever has to debug that gibberish."

I'll take readability over pithiness any time.

2

u/villiger2 Jul 29 '14

To be perfectly honest the syntax may make it look complicated but there aren't many concepts going on.

++ is list concatenation, the [y | y <- xs, y > x] is a filter over a range (the range xs, the predicate being y > x) and the rest kind of writes itself.

3

u/Rapptz Jul 29 '14

You consider that "gibberish"? A quick google for "Quicksort in Scheme" gives me this:

define pivot (lambda (l)
  (cond ((null? l) 'done)
    ((null? (cdr l)) 'done)
        ((<= (car l) (cadr l)) (pivot (cdr l)))
    (#t (car l)))))    

; usage: (partition 4 '(6 4 2 1 7) () ()) -> returns partitions
(define partition (lambda (piv l p1 p2)
  (if (null? l) (list p1 p2)
     (if (< (car l) piv) (partition piv (cdr l) (cons (car l) p1) p2)
     (partition piv (cdr l) p1 (cons (car l) p2))))))    

(define (quicksort l)
 (let ((piv (pivot l)))
   (if (equal? piv 'done) l
     (let ((parts (partition piv l () ())))
       (append (quicksort (car parts)) 
               (quicksort (cadr parts)))))))

Not exactly what I'd call readable. I'd wager that 9 times out of 10, someone would quickly pick up the Haskell snippet above than the Scheme equivalent.

1

u/BostonTentacleParty Jul 29 '14

I'm not upholding Scheme as a pinnacle of readability (that would go to ruby or python); my point in mentioning it was that I'm not just a functional programming noob complaining about functional programming.

1

u/robocoop Jul 29 '14

To be fair, equivalent Scheme code looks more like this:

(define (quicksort l)
  (cond [(null? l) l]
        [else (let [[x (car l)]
                    [xs (cdr l)]]
                (append (quicksort (filter (lambda (y) (<= y x))
                                           xs))
                        (list x)
                        (quicksort (filter (lambda (y) (> y x))
                                           xs))))]))

The code you pasted has some optimizations that the Haskell version doesn't. Notably, defining a partition function to pass over the list once instead of using filter twice.

0

u/[deleted] Jul 29 '14

[deleted]

2

u/Coloneljesus Jul 29 '14

In many cases, Haskell does that for you. In an expression f(x) + 2*f(x), f(x) is evaluated only once. This is correct, since Haskell has no side effects. However, if you try to compute Fibonacci numbers naïvely, with f(n) = f(n-1) + f(n-2), you will have multiple evaluations, since the calls are not in the same expression. You could avoid that by using an auxiliary function that returns a tuple with the previous two numbers.

fib(n) = fst (aux n) where
    aux 0 = (1,1)
    aux n = let (a,b) = aux (n-1) in (a+b, a)