Functional Programming

Functional Programming in Scala

Immutability

Why Functional Programming?

Lab

???

Step 1: Installing Scala

Step 2: The Scala Interpreter

  1. After you typed scala, you should have received a prompt of the Scala interpreter. Type 3 * 2. (Don't type the period, and hit ENTER after each input line.) What do you get?
  2. Type res0. What do you get?
  3. Type val a = 3 * 4. What do you get?
  4. Type a. What do you get?
  5. Type a = 9. What do you get?
  6. Type res0 = 9. What do you get?

Step 3: Functions are Values

  1. Type val triple = (x : Int) => 3 * x. What do you get?
  2. Type triple(5). What do you get?

    HINT: These “What do you get” exercises are a lot more effective when you and your buddy first discuss whar you think you'll get. “Form a hypothesis” is an essential part of the learning path.

  3. Type triple. What do you get?
  4. What is the type of triple in Scala?
  5. What is the type of 5 in Scala?

Step 4: Functions as Parameters

  1. Type 1 to 10. What do you get?
  2. Type (1 to 10).map(triple). What do you get? Why?
  3. How do you get a list of the squares of the numbers from 1 to 10?
  4. How do you get a list of the squares of the numbers from 1 to 10 without using val?