friki data migration

Ranch Hand

Posts: 772

posted 8 months ago

Get it from http://www.scala-lang.org/ Scalazine - First Steps to Scala The busy Java developer's guide to Scala - a series of articles on developerWorks by Ted Neward

Programming in Scala < 3 free sample chapters!

Scala Specs in PDF

Join the scala-user mailing list JavaRanch Scala Forum

To start with, here's a short Scala program that solves the first problem of Project Euler :

Scala is particularly well suited for problems that are defined recursively, or that can solved by recursive algorithms. Here are functions that calculate the factorial of a number (n!) and the numbers of the Fibonacci sequence.

This recursive "fibonacci" code is considered a "natural" implementation as it mirrors the mathematical definition of the function very closely. However this recursive version is very inefficient, and will become slow very quickly for growing n. Consequently it is not unusual in functional languages to replace inefficient recursive functions with equivalent tail recursive versions. Here is a tail recursive version of "fibonacci":

With this version the Scala compiler can detect the tail recursion and perform tail call optimization. The generated byte code will be no more inefficient than the code that is generated from the following imperative version.

CategoryLearnSomethingNew ScalaFaq

**Getting Started**

**Books**

**Specs**

**Get in the Loop**

**An example**

To start with, here's a short Scala program that solves the first problem of Project Euler :

*If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.*

**More examples**

Scala is particularly well suited for problems that are defined recursively, or that can solved by recursive algorithms. Here are functions that calculate the factorial of a number (n!) and the numbers of the Fibonacci sequence.

This recursive "fibonacci" code is considered a "natural" implementation as it mirrors the mathematical definition of the function very closely. However this recursive version is very inefficient, and will become slow very quickly for growing n. Consequently it is not unusual in functional languages to replace inefficient recursive functions with equivalent tail recursive versions. Here is a tail recursive version of "fibonacci":

With this version the Scala compiler can detect the tail recursion and perform tail call optimization. The generated byte code will be no more inefficient than the code that is generated from the following imperative version.

CategoryLearnSomethingNew ScalaFaq