1 - A gentle intro to Scala

In this lab, you get familiar with the basic Scala syntax and functionality.
You'll quickly get up to speed by solving some classical programming problems.

You can find today's contents here.

Practice

You can create a simple project, with a single Scala file. Call it whatever you like.
Along with the main method, you can create a method for every exercise.

  1. Fibonacci - write a function that computes the nth Fibonacci number.
    • write the signature of the method first and follow the syntax
       def methodName(param: ParamType, param: ParamType): ReturnType = {
        // code
      }
    • this should be a simple recursive function
  2. is it prime - write a function which tests if an integer is prime.
    • again, define the signature first
    • you might want to define an auxiliary function
    • you can do this inside the method implementation
  3. sum of powers - implement a function which computes a sum of powers for the numbers 1 to n
    • pay special attention to signature - you can make this very generic if you like
  4. @tailrec - rewrite a recursive function to be tail-recursive
    • follow our instructions on which function to rewrite and how to do this
    • you can probably rewrite one of the earlier functions to be @tailrec (wink)
sesiuni/scala/lab1.txt · Last modified: 2016/06/27 12:51 by dciocirlan