ListHigherOrderFunction takes an integer input from command line. Based on the input, write a code to generate a list intList, upto a given integer starting from 1. When input is 3, List(1,2,3) must be generated. Write a function factorial which gives the factorial of a given number using recursion. Write a higher order function myHigherOrderFunction which takes factorial function and intList, and prints the factorial of each integer in the intList
I have done the following code:…
object ListHigherOrderFunction{
def main(args :Array[String]){
val input = args(0).toInt
val intList :List[Int]=List.tabulate(3)(n=>n+1)
def factorial(n:Int): Int=
{
if(n == 1) 1
else n * factorial(n - 1)
}
def myHigherOrderFunction(factorial:()=>Int,list:List[Int]){
val result = for(i<-list){factorial(i)}
println(result)
}
myHigherOrderFunction(factorial, intList)
}
below is the error:…
$ scalac ListHigherOrderFunction.scala
ListHigherOrderFunction.scala:12: error: too many arguments for method apply: ()Unit in trait Function0
val result = for(i<-list){factorial(i)}
^
ListHigherOrderFunction.scala:17: error: type mismatch;
found : Int => Int
required: () => Unit
myHigherOrderFunction(factorial, intList)
^
two errors found