• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Alpha_ProgDes

Can any recursive function be a tail-recursive function?

7 posts in this topic

It's a random question that just popped into my head. It's a bit of a beginner question as well, admittedly. However, I read up on recursion on wikipedia and wondered if any function that is recursive can be tail-recursive. Also, since it's an "optimization" of sorts, should one always strive to write a tail-recursive function anytime they need to write a recursive function?
0

Share this post


Link to post
Share on other sites
Yes, a recursive function can always be re-written as a tail recursive function. However, that may involve reimplementing the stack as one of the parameters to the tail recursive function, so there isn't always a benefit to making it tail recursive.
1

Share this post


Link to post
Share on other sites
There are recursive functions that are not tail recursive. That is, not every recursive call is a tail call. A classical example is:
[CODE]
int fib(int n)
{
if (n < 2)
return n;
else
return fib(n-1) + fib(n-2);
}
[/CODE]

Note that the last operation is the addition of the results of the recursive calls, there are no tail calls to optimize.
1

Share this post


Link to post
Share on other sites
Anything can be rewritten an infinite number of ways, but in the purest sense, no not every recursive function can be rewritten tail-recursive instead. If it could then via a straightforward transformation we could change those functions to use iteration (which is generally more efficient) and have no use for recursion any more.
Using an explicit stack and tacking on some unrelated meaningless tail-recursion does not count as converting the algorithm to a tail-recursive implementation.
1

Share this post


Link to post
Share on other sites
Fibonacci cannot be written tail recursively as said, Some sorting algorithms that are written recursively I believe can't be tail recursive either.

If your writing a function and it absolutely has to be done recursively for whatever reason (there are a few instances where recursion is handy) and it can be made as a tail-recursive function then do it.

When we did recursion in computer science it took one of the school computers 45 minutes to solve the Fibonacci sequence for n = 40 written in VB.net on .net version 4. Impossible to time the iterative approach with what we had available.
-2

Share this post


Link to post
Share on other sites
Classic tree-traversal can't be tail recursive either. Even in the simplest case - a BSP tree - only one of the recursions can be a tail recursion; the other must be a regular recursion.
0

Share this post


Link to post
Share on other sites
I think it is unfortunate that the example of Fibonacci is what most people remember when thinking of recursive functions, since it's a toy example and it gives people the incorrect impression that recursion is slow for the wrong reasons. That example is just a terrible way to compute Fibonacci numbers, but that doesn't mean that using recursion is the problem.

Here is a very fast implementation of Fibonacci numbers using recursion:
[code]#include <iostream>

// M represents a matrix of the form
// (a b )
// (b a+b)
struct M {
long a, b;

M(long a, long b) : a(a), b(b) {
}
};

M operator*(M x, M y) {
return M(x.a*y.a + x.b*y.b, x.a*y.b + x.b*y.a + x.b*y.b);
}

// Fast exponentiation
M pow(M x, unsigned n) {
if (n==0)
return M(1,0);
if (n==1)
return x;
if (n%2==0)
return pow(x*x,n/2);
if (n%3==0)
return pow(x*x*x,n/3);
return x*pow(x,n-1);
}

// Compute
// (0 1)^n = (fib(n-1) fib(n) )
// (1 1) ( fib(n) fib(n+1))
long fib(unsigned n) {
M m = pow(M(0,1),n);
return m.b;
}

int main() {
std::cout << fib(40) << '\n';
}
[/code]

Depth-first search is the standard example that should come to mind instead. A recursive implementation is in this case the most natural, it's perfectly fast and small variations of it can be used for many purposes (e.g., to enumerate possibilities in many combinatorial problems, or to play chess). Edited by alvaro
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0