Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# Learn Scala

friki data migration
Ranch Hand
Posts: 772
Getting Started

• 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

• Books

• Programming in Scala < 3 free sample chapters!

• Specs

• Scala Specs in PDF

• Get in the Loop

• Join the scala-user mailing list
• JavaRanch Scala Forum

• 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