- What will be a type of the following function in curried form: (Int, String) => (Long, Boolean, Int) => String?
Int => (String => ((Long, Boolean, Int) => String)) or simplified Int => String => (Long, Boolean, Int) => String
- Describe the difference between a partially applied function and a partial function.
A partial function is not defined for some of the possible input values. A partially applied function has some of its parameters fixed to specific values.
- Define a signature and implement a function, uncurry, for a curried function of three arguments, A => B => C => R.
def uncurry[A,B,C,R](in: A => B => C => R): (A,B,C) => R = (a,b,c) => in(a)(b)(c)
- Implement a head-recursive function for the factorial calculation (n! = n * (n-1) * (n-2) * ... * 1.
def factorial(n: Long): Long = if (n < 2) n else n * factorial(n-1)
- Implement a tail-recursive function for a factorial calculation.
def factorial(n: Long): Long = {
def f(n: Long, acc: Long): Long = if (n < 2) acc else f(n-1, n * acc)
f(n,1)
}
- Implement a recursive function for a factorial calculation using trampolining.
import util.control.TailCalls._
def factorial(n: Long): TailRec[Long] = if (n<2) done(n) else tailcall(factorial(n-1)).map(_ * n)