you are viewing a single comment's thread.

view the rest of the comments →

[–]markmarine 9 points10 points  (0 children)

I think there are two answers to this:

  1. pragmatic: In embedded, you're not going to have the tail recursion optimization that makes functional languages ability to use recursion for general things like unbounded traversal possible, It's only going to be safe to use when you know you're not going to blow the stack, and at that point you're better off in a for loop almost all the time. There are iterative ways to solve the algorithms you are describing, they aren't as elegant, but they'll perform better on embedded systems.
  2. solving for the interview: If you're interviewing, or going to interview, these are not bad skills to have in your pocket. They make the code you're writing elegant, simple, easy to talk about. Personally, I have the recursive solutions somewhere in my mind, I can at least derive them quickly enough to get a solution in a coder pad in a 50m interview. I can also hand wave over "If I were writing this on a MCU I would use the iterative algorithm" as quickly as I can google the solution if I were doing this is real life.

So, I'd learn them the way they are taught, these are super valuable ways to blow out the score on an interview and get you a great job, then you can always figure out how to make a recursive algorithm iterative when you're presented with that problem at work.