0

I am learning Scala, but in most of the discussions, people pointing out that "Scala is bad for functional programming since it cannot handle tail calls, tail call is the backbone of Functional Programming".

Is that true?

sjrd
  • 21,805
  • 2
  • 61
  • 91
StandForTheRight
  • 135
  • 3
  • 14
  • 1
    Scala *does* support some forms of Tail Call *Optimization* during compilation - http://stackoverflow.com/questions/1677419/does-scala-support-tail-recursion-optimization?rq=1 , http://stackoverflow.com/q/24355193/2864740 , http://stackoverflow.com/questions/9168624/why-is-my-scala-tail-recursion-faster-than-the-while-loop?rq=1 – user2864740 Sep 17 '15 at 09:08
  • first: please don't *SCREAM* that much - it's annoying - to your question: AFAIK Scala cannot handle all kinds of tail-calls (I think it only accepts recursive calls - so mutual calls won't work) and of course this will not really matter if you don't write to much loop-like recursive functions - instead use the HOF (a good advice for all FP languages out there) - they are optimized to handle this stuff for you – Random Dev Sep 17 '15 at 09:10
  • Thank You, Carsten, for your reply Sorry if my question makes you annoy – StandForTheRight Sep 17 '15 at 09:43
  • @DbasePlsql not your question - only the CAPs (someone edited out) - sorry but you cannot edit your comments after a few minutes so just ignore the first part – Random Dev Sep 17 '15 at 09:47
  • ... in case you don't know what that is (I messed up both the *the* and the casing): I was talking about *higher-order-functions* - things like `map`, `fold/`, ... – Random Dev Sep 17 '15 at 09:54

1 Answers1

-1

This is incorrect. Scala is able to handle tail calls.

You can even annotate functions with scala.annotation.tailrec. In this case the compiler will throw an error if the function is not tail-recursive. This way you can ensure, that the optimization really is applied.

Richard
  • 1,117
  • 11
  • 31
  • 2
    `tailrec` is not the same as tail-call - see if you call another function in tail-call-position - so there is a big class of functions that other languages are able to translate into non-stack-eating monster using TCO that Scala cannot handle - I think the usual suspect that will *burst* is `even 0 = true`, `even n = odd (n-1)`, `odd 0 = false`, `odd n = even (n-1)` - this ofc is not scala-code (but it should be easy to translate) - you get the idea I hope – Random Dev Sep 17 '15 at 09:33
  • `tailrec` is called "tailrec", precisely because it *isn't* about tailcalls but about tail-recursive calls. More precisely, *direct* tail-recursive calls. – Jörg W Mittag Sep 17 '15 at 09:45
  • Thank you Richard, Now I need not think this as an issue :) – StandForTheRight Sep 17 '15 at 09:45
  • @DbasePlsql it can be an issue but you will probably never notice it in your daily work – Random Dev Sep 17 '15 at 09:49