I’ve been bouncing around functional languages for the last few months from F# to Haskell to Scheme (Racket). I’ve never really used recursion much, but Haskell and its pattern matching really helped me to be less afraid of them. Now that I’m using Scheme, I seem to default to recursive methods. I’m curious if this is indicative of just going through an “ooo shiny!” phase or if recursion is a staple of Scheme development.
Side note: I’ve been shooting for tail recursion whenever I write recursive methods.
I think it depends what type of recursion you’re talking about.
The first eye-opener (for instance if you’re working through SICP) is that iteration can be replaced by recursion. All those lines of tedious and error-prone loop code you’ve written in a past life, can be done another way. This is a cool and (as you put it) “ooh shiny!” experience.
The next eye-opener is how little of that sort of recursive code you’ll actually write in real life. Instead, you’ll avoid tedious iteration — and tedious recursion, both — by using building blocks like
mapandfold.Furthermore in Racket you’ll probably graduate from
mapandfoldto preferring the “comprehensions” likefor/list,for/vector, andfor/foldthat work on sequences not just lists. Up the food chain you keep going.Having said that, there are some problems you will best solve recursively (not just “iteration by other means”). And the comfort level you got in eye-opener number one, will help there, I think.