Functional Programming: Append (A: List (Int), B: List (Int) ) : List (Int) Reverse (A: List (Int) ) : List (Int)
Functional Programming: Append (A: List (Int), B: List (Int) ) : List (Int) Reverse (A: List (Int) ) : List (Int)
Functional Programming
Start Eclipse and make a new Scala project: File -> New -> Project -> Scala Wizards -> Scala
Project -> Next. Give a name lab1 and click Finish. Right-click on the project in the Package
Explorer, then select Scala -> Create Scala Interpreter.
1.
a) How do you get the squares of the numbers from 1 to 10?
b) How do you get the squares of the numbers from 1 to 10 without using val? Hint:
Anonymous functions
2. Write a function that check whether the number is even.
3. Write a Scala function pow(x : Double, n : Int) : Double that return a
power xn. Hint: think recursively.
For example:
pow(2,0) return 1
pow(2,3) return 8
4. Make a list of the elements 1, 2, 3, 5, 7.
Write 2 functions (dont use Scala library functions: append, reverse):
append(a : List[Int], b : List[Int]) : List[Int]
reverse(a : List[Int]) : List[Int]
5. Write a Scala function greaterThan that returns a list of all numbers > given number in a
given list, following the sample code
def greaterThan(n : Int, lst : List[Int]) = {
val fun = ... // your work
lst.filter(fun)
}
For example, greaterThan(50, List(1, 55, 6, 2)) yields List(55)
Finish greaterThan function.
6. Curry the arguments in greaterThan function so that the following invocation is legal:
val filter = greaterThan(5) _
val result = filter (List(1, 2, 3, 4, 5, 6, 7) // List(6, 7)