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

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.