Questions tagged [tail-recursion]

Tail recursion is a recursive strategy in which a function does some amount of work, then invokes itself. The "tail" refers to the fact that the recursion is at the very end of the function. Many -- especially functional -- programming language compilers can turn these types of calls into iteration, meaning tail recursion in supported languages can be used without fear of a stack overflow, regardless of the number of calls.

Tail recursion is a recursive strategy in which a function does some amount of work, then invokes itself. The "tail" refers to the fact that the recursion is at the very end of the function. Many (especially functional language) compilers can turn these types of calls into iterative calls, meaning tail recursion can be used without fear of a stack overflow, regardless of the number of calls.

1348 questions
8
votes
3 answers

Why can't scalac optimize tail recursion in certain scenarios?

Why doesn't scalac (the Scala compiler) optimize tail recursion? Code and compiler invocations that demonstrates this: > cat foo.scala class Foo { def ifak(n: Int, acc: Int):Int = { if (n == 1) acc else ifak(n-1, n*acc) } } > scalac…
IttayD
  • 28,271
  • 28
  • 124
  • 178
8
votes
4 answers

Common Lisp: Why does my tail-recursive function cause a stack overflow?

I have a problem in understanding the performance of a Common Lisp function (I am still a novice). I have two versions of this function, which simply computes the sum of all integers up to a given n. Non-tail-recursive version: (defun addup3 (n) …
oarfish
  • 4,116
  • 4
  • 37
  • 66
8
votes
1 answer

Tail recursion in gcc/g++

I've tried to search, but was unable to find: what are requisites for functions so that gcc would optimize the tail recursion? Is there any reference or list that would contain the most important cases? Since this is version specific, of my…
dtldarek
  • 949
  • 1
  • 13
  • 17
8
votes
5 answers

How to simplify nested-if using to return value in Haskell

I want to check the condition of the previous if condition to determine the next if condition is to be executed or not. Each if condition may return a value. Edit: Sorry for that the example I provided before look a bit odd...:( This is my real…
8
votes
1 answer

Tail recursion and exceptions in F#

I've been googling for ages and still can't find the answer. From what I understand F# 3.0 runnning on .NET 4.5 will not use tail recursion for a recursive method if the invoker has wrapped the call in a try/catch and/or try/finally block. What is…
user1563526
  • 275
  • 2
  • 8
8
votes
3 answers

Are tail recursion and dynamic programming the same?

I was programming Fibonacci numbers using tail recursion and it seems like the idea behind it is same as Dynamic programming. So are they same? or rather there is some amount of similarity between them? If not when are the cases when they get…
gizgok
  • 7,303
  • 21
  • 79
  • 124
8
votes
5 answers

Does using a lot of tail-recursion in Erlang slow it down?

I've been reading about Erlang lately and how tail-recursion is so heavily used, due to the difficulty of using iterative loops. Doesn't this high use of recursion slow it down, what with all the function calls and the effect they have on the stack?…
samoz
  • 56,849
  • 55
  • 141
  • 195
7
votes
4 answers

Tail Recursion in C++ with multiple recursive function calls

I was reading this post on tail recursion. I'll copy the posted solution: unsigned int f( unsigned int a ) { if ( a == 0 ) { return a; } return f( a - 1 ); // tail recursion } I was wondering, what about if the result depends on…
Heisenbug
  • 38,762
  • 28
  • 132
  • 190
7
votes
3 answers

2 questions at the end of a functional programming course

Here seems to be the two biggest things I can take from the How to Design Programs (simplified Racket) course I just finished, straight from the lecture notes of the course: 1) Tail call optimization, and the lack thereof in non-functional…
7
votes
2 answers

Erlang: Can this be done without lists:reverse?

I am a beginner learning Erlang. After reading about list comprehensions and recursion in Erlang, I wanted to try to implement my own map function, which turned out like this: % Map: Map all elements in a list by a function map(List,Fun) ->…
driis
  • 161,458
  • 45
  • 265
  • 341
7
votes
3 answers

Implementing a tail recursive version of quicksort-like function in F#/OCaml

Is it possible to implement a tail recursive version of the quick sort algorithm (via the continuation pattern)? And if it is, how would one implement it? Normal (not optimized) version: let rec quicksort list = match list with | [] -> [] |…
Yet Another Geek
  • 4,251
  • 1
  • 28
  • 40
7
votes
3 answers

Why scala @tailrec can't be used on Option.flatMap?

In scala, the following 2 functions serve exactly the same purpose: @tailrec final def fn(str: String): Option[String] = { Option(str).filter(_.nonEmpty).flatMap { v => fn(v.drop(1)) } } @tailrec final def fn2(str: String): Option[String] =…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
7
votes
2 answers

Why is this tail recursive?

check out this Scala-code: def rec(n: Int) { if (n > 1) { val d = n / 2 rec(d) // if (d > 1) // abort loop rec(n/d) } } This code will result in an endless loop. Owing to tail recursive optimization I don't get a…
kiritsuku
  • 52,967
  • 18
  • 114
  • 136
7
votes
3 answers

Doesn't OCaml have any recursion checks?

I've been playing with OCaml recently, and promptly did my favourite thing to check how well developed a VM/Compiler really is, and wrote a recursive Program: let rec f i = Printf.eprintf "i = %d\n" i; f(i+1);; let () = f 0;; The…
user350814
7
votes
3 answers

F# vs. C# performance Signatures with sample code

There are many discussions on this topic already, but I am all about flogging dead horses, particularly when I discover they may still be breathing. I was working on parsing the unusual and exotic file format that is the CSV, and for fun I decided…
Snark
  • 1,664
  • 14
  • 27