0

This function is taking list and one of two functions below and have to match every element in list.

val sqrt = (x: Double) => x * x val doubleValue = (x: Double) => 2 * x val yoman = (list: List[Double], func: Function[Double, Double]) => list match { case head :: tail => func(head) :: yoman(tail, func) case Nil => list } yoman(List(3.0, 4.0, 99.0), sqrt) yoman(List(-5.0, 1.0, 9.0), doubleValue) 

Now I have a problem with first case:

Error:(5, 39) recursive lazy value yoman needs type case head :: tail => func(head) :: yoman(tail, func) ^ 
1
  • Why is yoman a val containing a lambda instead of a def? Commented Oct 21, 2018 at 13:50

1 Answer 1

1

for recursive functions you need to specify the return type

 val yoman: (List[Double], Function[Double, Double]) => List[Double] = (list: List[Double], func: Function[Double, Double]) => list match { case head :: tail => func(head) :: yoman(tail, func) case Nil => list } 

as you can see, you declare yoman, followed by the type (function that received a list and another function, and returns a list double), and then the body itself

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.