3 ms·
> Recursion is equivalent to iteration. You can solve any problem you like with it. Maybe my comment was somehow badly written, because you (and the only oth
by the5avage 6y ago
> Recursion is equivalent to iteration. You can solve any problem you like with it.
Maybe my comment was somehow badly written, because you (and
the only other reply from chriswarbo) clearly misunderstood me.
I know that recursion is equivalent to iteration^^
My point was that some problems fit the one or the other better.
Look at fft for example. The algorithm is easy with recursion but hard with iteration. For other problems its the opposite.
- tasuki 6y agoWhich problems are hard to solve with recursion instead of iteration?
- the5avage 6y agoI found something like splitting a string into words not very intuitive... Also the classical (recursive) fibonacci function everyone shows has accidental complexity of O(2^n). If you want a sane version it looks definitely more complicated than the iterative version. I never saw a recursive version of bresenham's line plotting algorithm so this might be a good candidate as well ;)
- chongli 6y agoI found something like splitting a string into words not very intuitive... Just use a couple of accumulators (one for the current word, one for the list of completed words). Also the classical (recursive) fibonacci function everyone shows has accidental complexity of O(2^n). That's moving the goalposts. If you want a really fast fibonacci function then the classical iterative algorithm [which is O(n)] is not what you want either. Better to use matrix exponentiation which you can implement nicely with recursion using a divide-and-conquer approach similar to mergesort, giving you fibonacci with time O(log(n)). I never saw a recursive version of bresenham's line plotting algorithm so this might be a good candidate as well ;) There's nothing special about Bresenham's line algorithm that would make it particularly challenging to implement with recursion.
- a1369209993 6y ago> splitting a string into words not very intuitive It seems okay? split :: String -> [String] split s = w s "" where w [] "" = [] w [] a = [reverse a] w (' ':ss) "" = w ss "" w (' ':ss) a = reverse a : w ss "" w (c:ss) a = w ss (c:a) If you're done then you're done (and dump the accumulator), if it's a space get rid of it (and dump the accumulator), otherwise add it to the acuumulator. That the accumulator is stored backward is a bit wierd, but seems more a consequence of linked lists than recursion per se.
- the5avage 6y agoYeah it's ok ;) Going back to my original comment this code is exactly what I meant with "patterns to deal with other problems". You clearly used a pattern (accumulator) with wich you are familiar. Do you agree that most imperative programmers would not come up with this solution on their own, if you didn't teach them the "accumulator pattern"? For me personally a tutorial with a lot code like that would be interesting. Showing me another version of a recursive descent parser doesn't teach me anything, because I know how to write it (with recursion!!!) in an imperative language...
- chongli 6y agoDo you agree that most imperative programmers would not come up with this solution on their own, if you didn't teach them the "accumulator pattern"? Background: I’m currently working as a full time assistant for the first year computer science course at my university. We have around 1800 people enrolled in the class including over 1000 first year CS students. Many of our first years were exposed to imperative programming in high school, either at home or with a high school CS class. The course we teach is based on the book HTDP and uses a purely functional teaching dialect of Racket. Many of our students struggle to figure out how to use the different recursion patterns such as accumulative recursion and mutual recursion. I’m not the GP but I will answer your question anyway: yes, I agree that most imperative programmers would not come up with that solution. I have witnessed it myself first hand. Where I disagree is with any implication or suggestion that they should, given no prior functional programming experience. Accumulative recursion is a standard pattern in functional programming but if you’re an imperative programmer you may not have ever needed to learn it. Therefore, I think it would be unreasonable to expect you to come up with it on the fly.