Answer:
Explanation:
val db = ("John", "x3456", 50.1) :: ("Jane", "x1234", 107.3) ::
("Joan", "unlisted", 12.7) :: Nil
type listOfTuples = List[(String, String, Double)]
def find_salary(name: String) = {
def search(t: listOfTuples): Double = t match {
case (name_, _, salary) :: t if name == name_ => salary
case _ :: t => search(t)
case Nil =>
throw new Exception("Invalid Argument in find_salary")
}
search(db)
}
def select(pred: (String, String, Double) => Boolean) = {
def search(found: listOfTuples): listOfTuples = found match {
case (p1, p2, p3) :: t if pred(p1, p2, p3) => (p1, p2, p3) :: search(t)
case (p1, p2, p3) :: t if !pred(p1, p2, p3) => search(t)
case Nil => Nil
case _ => throw new Exception("Invalid Argument in select function")
}
search(db)
}
println("Searching the salary of 'Joan' at db: " + find_salary("Joan"))
println("")
val predicate = (_:String, _:String, salary:Double) => (salary < 100.0)
println("All employees that match with predicate 'salary < 100.0': ")
println("\t" + select(predicate) + "\n")