3 - Functional Programming Basics

Now we're talking!
Today we go through the elementary functional programming weapons you need to write functional Scala.

You can find today's contents here.

Practice

As before, you can create a simple project, starting with a single Scala file.
You'll probably need to define new classes/objects, which we recommend you add in other files.

  1. Use Lists for this exercise. We want you to work with functional primitives - applications on Sets and other collections have the same principles.
    • extract only prime numbers from a List
    • given a List of integers, return a new list with the number of digits of each element
    • given a List of integers, return a new list which contains the integers and their String representation; example:
       List(1,2) -> List(1, "one", 2, "two")
  2. implement a Cons-based List of integers
    • create two case classes: Nil for an empty list, and Cons which has a head (integer) and a tail (the rest of the list)
      • there are some important questions you should ask first before you start coding…
    • implement map, flatMap and filter
      • watch for the signatures first
      • implement recursively (bonus for who does tail recursion ;) )
      • you might want to implement some auxiliary functions
    • create an instance of a Cons-based List you've just implemented
    • extract only the prime numbers from your list, then count the number of digits for each
      • use a for-comprehension!
sesiuni/scala/lab3.txt · Last modified: 2016/06/29 17:12 by dciocirlan