Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I think recursion is easier to read than C-style for-loops because you don't need to memorise syntax.

for (int i = 0; i < 5; i++) {...}

vs

```let rec func_name counter = if counter = 5 then print "blast off!" else func_name (counter + 1)```

I don't really need to memorise the order of operations compared to a for loop (initialise, exit condition, increment) and I don't need to memorise if the exit condition means "execute while this is true" or "break the loop if this is true". That's just me though. I think the syntax for recursion is easier but loops might conceptually be easier to understand.

I think, better than both of those for the general case (and a good introduction) are "fold" functions and "foreach"-style loops, which iterate over every element in a list. I think those are used more often, and the reason you may want to do this is clearer to a student.



With beginners, there's a severe "working memory" limitation from the fact that they have no compression that comes from tying things together.

The for line ends up being one independent line they can grok, then free from their memory when looking inside the loop, knowing that I gets bigger for a while. From my experience, something like the recursion will blow away a beginners working memory, because they can't piecewise it. But, the biggest problem is the for loop is trivially expanded with the exact same format, you just shove stuff into the body. The recursion method requires significant reworking to expand. Beginners appreciate simple templates that they can understand and modify, because they're still putting it all together.


Aren’t you leaving out the initialization in your recursive example though?


You're right. I didn't notice that, but I guess the initialisation would be the caller function's responsibility or there would be a wrapper function with a default initialisation value (maybe with the recursive example nested inside the wrapper).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: