Scala has smart compiler?

I made recursive function, just like

require : L (List[Int])

L pattern matching

  1. Nil => Thread.dumpStack()
  2. x :: xs => print(x) + function(xs)
def function(L : List[Int]) {
    L match {
        case Nil => Thread.dumpStack()
        case x :: xs => print(x + " "); function(xs)
    }
}

val l = (1 to 5).toList // function(l)

So I think this function in the stack frame n times, but it occurs one time, I think this function has already found Nil and print out exception Thread.dumpStack.

Is scala compiler smart or other else?

Answers


You are observing tail recursion: there is nothing to store from one iteration to the next, so the recursion is essentially turned into a while loop by the compiler. (So, yes, the compiler is smart in that way.)


Need Your Help

Automatically Convert SVG to Android-Usable PNG

svg command-line imagemagick imagemagick-convert material-design

I have about 434 .svg icon I would like to get ready for using them in Android Apps. Since it would probably be insane to export them all manually using Inkspace, I am trying to find a way to expor...

AngularJS, filters: how to turn a big giant object (JSON) into an array?

javascript arrays json angularjs

I'm having some issues with the Angular filters, according to the docs the filter only works with arrays, I have now a very big object, is actually to display some dynamic content. I will leave you...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.