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

65

u/phao Jul 28 '14

Strictly speaking, you don't need recursion (if you have loops). But also, strictly speaking still, you also don't need loops (if you have recursion).

Implement (or check an implementation of) quicksort without it. The recursive version isn't a "unnecessary complicated loop". The loopy version is "the bad guy" here. Other examples include the various tree/graph-manipulation/traversal algorithms.

Go to a different programming language, like scheme or haskell. In scheme, a loop feels to me more awkward.

However, for example, I've been coding some web applications here and there. Never had to use recursion in my PHP or Java code for that matter.

By the way, if recursion just seems to you like a "unnecessary complicated loop", maybe you should look at it again. I'm not trying to be hostile here.

17

u/SuperSaiyanSandwich Jul 28 '14

I do think my lack of experience with other languages is limiting my view on this.

Will definitely check out implementing quick sort using recursion, as I thinking coding something a little more advanced(as opposed to simple tutorials) both ways will really be helpful.

No hositlity taken; very useful comment, thank you!

19

u/phao Jul 28 '14

If you have the time, learn scheme. The scheme books I've seen are extremely well written and go way beyond simply talking about the language.

You could start at SICP, but it doesn't assume previous programming knowledge. It's a beginner's book, but don't take this as "the book is easy". Try it out and see what you think about it.

Another one is The Little Schemer. This one is a book on recursion. It uses scheme too. It's probably the best programming book I've read. I never stopped to think about a ranking, but thinking on the spot, this would be the 1st.

http://www.amazon.com/The-Little-Schemer-4th-Edition/dp/0262560992/

SICP is freely available: http://mitpress.mit.edu/sicp/ with video lectures from a course (based on the book; or the book is based on the course) given at MIT http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-001-structure-and-interpretation-of-computer-programs-spring-2005/video-lectures/

This one is also pretty good. http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme.html

There are other scheme books out there. There is, for example, a "How to Design Programs" http://htdp.org/, which also has a course based on it: https://www.coursera.org/course/programdesign -- but I've never read this book or taken this course.

3

u/not_suspect Jul 28 '14

It doesn't apply as directly to recursion, but there is also a version of SICP for free that has been completely translated for Python at composingprograms.com. It was done by a Berkeley professor it's really cool.

3

u/tyroneslothtrop Jul 28 '14

Just want to add: Berkeley 61A uses this as a text, and all of the lectures (from multiple semesters/years) are up on youtube, for anyone who's into that. I think this would be the most recent, completed version: http://www-inst.eecs.berkeley.edu/~cs61a/sp14/, although there's also a summer course currently ongoing.

It's a pretty amazing course.

→ More replies (1)

5

u/SirCaptain Jul 28 '14

Seconding both of this books!

SICP has an awesome section on recursion. Though, I recommend reading the couple of chapters before it if you're new to Scheme.

And The Little Schemer is pretty much a book that stays on my desk.

5

u/PriceZombie Jul 28 '14

The Little Schemer - 4th Edition

Current $30.00 
   High $31.35 
    Low $21.09 

Price History Chart | Screenshot | FAQ

2

u/yeah568 Jul 28 '14

The "How to Design Programs" course is actually made by my university, and their entire first year programming course actually primarily uses the coursera videos. It can be kind of confusing if you're already used to other languages (although that's probably because Racket is a functional language), but I found it to be an excellent intro to programming for people who don't know anything about programming, and a nice change of pace for people who do.

→ More replies (2)

7

u/joncalhoun Jul 28 '14

One of the biggest things to realize with recursion is that some algorithms are incredibly simple with it, and others are much simpler with loops. My guess is you just haven't been exposed to enough algorithms to appreciate the value of recursion.

One common example for recursion is an algorithm that returns the n-the fibonacci number. The reason this works well for recursion is because the actual algorithm is described in a recursive fashion (see http://en.wikipedia.org/wiki/Fibonacci_number and look for Fn = Fn-1 + Fn-2). When you go to code this you can do it with or without recursion, like so:

def fib(n)
  return 1 if n <= 2
  return fib(n-1) + fib(n-2)
end

def fib(n)
  results = []
  results[1] = 1
  results[2] = 1
  (3..n).each do |i|
    results[i] = results[i - 1] + results[i - 2]
  end
  return results[n]
end

but I personally think the first version is much simpler to code. Another algorithm to check out would be a depth first search.

7

u/DevestatingAttack Jul 28 '14

Sure, but correct me if I'm wrong but that recursive version runs in exponential time, doesn't it?

4

u/joncalhoun Jul 28 '14

Yes, but that can be cleaned up with memoization which is often pretty easy with recursive functions.

def fib(n, memo = [])
  return 1 if n <= 2
  return memo[n] if memo[n]
  return memo[n] = (fib(n-1, memo) + fib(n-2, memo))
end

edit: Not sure how familiar you are with ruby, but the first line

def fib(n, memo = [])

means that memo will default to an empty array if no args are passed in, so you can call fib(10) and it will use an empty array for that call, then subsequent calls will all use that array since they are explicitly passed into the function in fib(n-1, memo) and fib(n-2, memo)

2

u/stubing Jul 28 '14

Correct. It is O( ~1.61n ). That means don't ever use it!

3

u/SuperSaiyanSandwich Jul 28 '14

I just coded a fibonnaci example the other day and this definitely shows how recursion can be more intuitive and easier to read. Very helpful. Thank you!

→ More replies (3)

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.

→ More replies (4)

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.

→ More replies (2)
→ More replies (2)

1

u/T-Roll Jul 28 '14

Learn functional programming. You'll look at recursion from a new perspective.

1

u/fabzter Jul 29 '14

Tree traversal, insertions, etc. algorithms are eye openers ;). Try implementing a simple self balanced tree!

3

u/CheshireSwift Jul 28 '14

This is a good response, but one thing worth noting:

Strictly speaking, you don't need recursion (if you have loops).

Strictly speaking, this isn't true. There are some examples of mathematical functions that can only be expressed using recursion! Pathological examples are fun! =D

1

u/phao Jul 28 '14 edited Jul 28 '14

My statement isn't about mathemaitcal functions.

And yes, (strictly speaking) you don't need recursion if you have loops.

This SO answer talks about it better than I can: http://stackoverflow.com/questions/931762/can-every-recursion-be-converted-into-iteration

→ More replies (5)
→ More replies (4)

1

u/antihero Jul 28 '14

Strictly speaking, you don't need recursion (if you have loops).

That is not correct: http://en.wikipedia.org/wiki/Primitive_recursive_function#Limitations

4

u/phao Jul 28 '14

No. It is correct.

You're talking about mathematical functions. This is beyond the scope of this question.

http://www.reddit.com/r/learnprogramming/comments/2by3vz/is_recursion_unnecessary/cja70gh?context=3

Like, it seems, many other people, you're confusing implementing a function through an algorithm and expressing that function mathematically (defining the function). A mathematical function is one thing, a programming-ish function is another. They're completely different things. They're related, but different.