Recursion
Exercise 1 sum
Write a function that takes a positive integer n and returns the sum of all the integers from 1 to n.
def sum(n: Int): Int =
// TODO
// example usage
sum(5) // returns 15
Exercise 2 sumBetween
Write a function that takes two integers and returns the sum of all the integers between them, including the endpoints.
def sumBetween(x: Int, y: Int): Int =
// TODO
// example usage
sumBetween(1, 5) // returns 15
Exercise 3 sumList
Write a function that takes a list of integers and returns the sum of all the integers in the list.
def sumList(list: List[Int]): Int = {
// TODO
}
// example usage
sumList(List(1, 2, 3, 4, 5)) // returns 15
Exercise 4 filter even
Write a function that takes a list of integers and returns a new list with all the even numbers.
def filterEven(list: List[Int]): List[Int] =
// TODO
// example usage
filterEven(List(1, 2, 3, 4, 5, 6)) // returns List(2, 4, 6)
Exercise 5 longest string
Write a function that takes a list of strings and returns the length of the longest string in the list.
def longestString(list: List[String]): Int =
// TODO
// example usage
longestString(List("apple", "banana", "orange", "pear")) // returns 6