Good resources for learning about recursion [duplicate]

Possible Duplicate:
Resources for improving your comprehension of recursion?

I am a programmer with 2 years experience, and sometimes think I could solve a specific problem with recursion, but in most cases I fail miserably.

I am asking your advice about resources for learning, refreshing and deepening one’s knowledge about recursion, preferably:

  • helping to identify when you could use recursion.
  • practical examples (applied to contemporary programming languages).
  • a reference handbook.
  • source code.

9

Spotting when to use recursion is actually quite easy – the key concept is noticing that part of the problem is a smaller version of the whole problem. Examples:

  • Functions on lists can be written using recursion if you see that you can achieve the result by doing something with the first element of the list and then calling the function recursively on the rest of the list. Here the “smaller version of the whole problem” is the rest of the list excluding the first element.
  • Functions operating on binary trees can often use recursion by recursively calling the function on the left half of the tree and on the right half of the tree and combining the results in some way. Here the “smaller version of the whole problem” are the left and right sub-trees.
  • Mathematical functions defined recursively (e.g. a function to generate the fibonacci numbers F(n) = F(n-1) + F(n-2) ) often have a simple recursive implementation that mirrors the mathematical definition. Here the “smaller version of the whole problem” are the two previous numbers in the fibonacci sequence.

If you really want to learn about recursion, then picking up a functional language is a good idea – recursion is a natural programming style in functional languages and you’ll be steered away from falling back on “imperative” solutions. I’d suggest a Lisp-1 (e.g. Scheme or Clojure) in particular: some of the recursive functions defined on lists are things of beauty.

1

I’m not aware of any recursion-specific books, but it’s covered thoroughly in almost any intro to algorithms book.

As far as when you can use recursion, you could use it on practically any algorithm. In some functional programming languages, you must use recursion for things other languages use loops for. If you want to practice recursion, learning a functional language is a good step, and you will find tons of recursion examples in their tutorials.

When it’s beneficial to use recursion is another question. Two situations come to mind. The first is when you find yourself using a stack for a data structure. In that situation using recursion automatically gives you a stack, so you don’t have to worry about those details. The other situation is when you have a large number of nested loops, or an unknown level of nesting. Recursion makes nesting loops very easy.

The trick to understanding recursion is to not try to hold the entire stack in your head. Focus on just one small part at a time. Think about your current state and trust that the deeper levels are working as advertised.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật