• 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
Jethro_T

Tail Recursion

7 posts in this topic

Hey guys, I have a function that I implemented easily using regular recursion but I've been trying to figure out how to write it using tail recursion instead, I can't seem to wrap my head around it though at all.

 

The function is:

 

if n < 4:

f(n) = n

 

else:

f(n) = f(n - 1) + 2(f(n - 2)) + 3(f(n - 3)) + 4(f(n - 4))

 

Can anyone help me figure out how to solve this using tail recursion?

 

 

0

Share this post


Link to post
Share on other sites
Start out by writing it with standard recursion. We'll go from there.

Are you wanting to explicitly perform tail recursion (with a goto or etc) or are you wanting to trigger the compiler's tail recursion optimization behavior? Edited by Khatharr
0

Share this post


Link to post
Share on other sites
First the obligatory statement that this is a terrible example to use tail recursion with. To transform this into a tail recursive solution you need to manually recreate the stack that the compiler will generate for you. If you wish to preserve the recursive algorithm used here (which is terribly inefficient) then pretend you are the compiler for a moment. To evaluate the recursive calls you would need a temporary accumulator variable, which you would then add in the values of each of the calls. First you would add in 1 * f(n - 1) then 2 * f(n - 2), etc. So there you have two variables for your tail recursive calls: one the accumulator and one of the stack of multiples and function arguments. Each tail recursive call would see if the stack is empty and if so either add to the accumulator or the stack, or otherwise consume a value from the stack and process that.
1

Share this post


Link to post
Share on other sites
This doesn't answer the original question, but this is how I like to think of sequences like this one.

Take the matrix
    0 1 0 0
M = 0 0 1 0
    0 0 0 1
    4 3 2 1

and the vector
    f(n-4)
v = f(n-3)
    f(n-2)
    f(n-1)

When you multiply M*v you get the vector
    f(n-3)
    f(n-2)
    f(n-1)
    f(n)

So the indices are pushed by one in this operation. If you want to advance two steps at once, use M^2.

Now, in order to compute f(n), compute M^n, and multiply the result by the first four terms (0 1 2 3). Computing a power of a matrix can be done in O(log(n)) multiplications. You can also make this computation more efficient by realizing that the matrices involved all live in a 4-dimensional linear subspace, and you can define a special type that contains 4 numbers with the appropriate operations (this part is a bit tricky).

This method is not only computationally efficient, but it gives you a much deeper understanding of the situation. For instance, if you want to find an explicit formula for f(n), you just need to diagonalize M. Edited by Álvaro
0

Share this post


Link to post
Share on other sites

I solved my problem this morning before I got a chance to get on the computer to check the replies to my post.  Seemed very easy this morning when I thought outloud "The value of f(n) is the value of the last 4 values of f(n)."  Naturally this made me realize immediately that I simply needed to start from the bottom 4 cases and build up the value of the function by simply keeping track of the previous 4 values of f(n).

 

My implementation in scheme:

[source]

(define (q2iterative n)
  (define (q2iter n a b c d)
    (if (= n 0)
      a
     
      (q2iter (- n 1) (+ a (* b 2) (* c 3) (* d 4)) a b c)
    )
  )
 
  (if (< n 4)
      n
     
      (q2iter (- n 3) 3 2 1 0)
  )
)

[/source]

0

Share this post


Link to post
Share on other sites
Well, I don't see how that's tail recursion. That's just an iterative solution (which makes much more sense than the recursive one that they insist on teaching in school).
1

Share this post


Link to post
Share on other sites
<blockquote class="ipsBlockquote" data-author="Álvaro" data-cid="5024130"><p>Well, I don't see how that's tail recursion. That's just an iterative solution (which makes much more sense than the recursive one that they insist on teaching in school).</p></blockquote>???<br />q2iter is tail recursive. Calls itself at the end, and only at the end.<br />Of course this is also an iterative solution. Tail recursion is how you do iteration in Scheme.
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