Using Higher Order Function With List

Trying to do this lab exercise with no luck…can someone help me please

ListHigherOrderFunction takes an integer input from command line. Based on the input, write code to generate a list intList , upto given integer starting from 1 i.e 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 which prints the factorial of each integer in the intList

Copy the following ListHigherOrderFunction program into the file:

object ListHigherOrderFunction{
def main(args :Array[String]){
val input = args(0).toInt
val intList :List[Int] //complete the code
def factorial //complete the code
def myHigherOrderFunction //complete the code
myHigherOrderFunction(factorial, intList)
}
}`

I am not sure whether it is correct to give solution for your lab exercise. I guess that it is lab exercise and not home work exercise. So I think now nothing wrong in giving the solution.

object ListHigherOrderFunction {

def main(args: Array[String]): Unit = {
val input = args(0).toInt
val intList: List[Int] = (1 to input).flatMap((x: Int) => List(x)).toList

def factorial(num: Int): Int = {
def fac(x: Int, acc: Int): Int = {
if (x == 1) acc
else fac(x - 1, acc * x)
}

fac(num, 1)
}

def myHigherOrderFunction(fac: (Int) => Int, intList: List[Int]) = intList.foreach((x: Int) => println(fac(x)))

myHigherOrderFunction(factorial, intList)
}

}

1 Like

Thanks a Ton Buddy, this reallly helped with no issues reported…Really appreciate your assistance

Can you kindly evaluate the code and let me know where Im messing as I’m unable to mark it as complete …

flatMapUsage takes integer input from command line. A list must be generated with the input number of elements in the list starting from one i.e when input is 3 List(1,2,3) must be generated. Each number in the List generated i.e numberList = List(1,2,3) must be used to create seperate lists with elements equal to the number starting from 1, i.e when input is 3; List(1), List(1, 2), List(1,2,3) must be created and joined together to give output resultList as List(1, 1, 2, 1, 2, 3) Put the file in insert mode with clicking i .Output will be: List(1,1,2,1,2,3) 6

object flatMapUsage{
def main(args :Array[String]){
val input :Int = args(0).toInt
val numberList = List(1, 2, 3)
def listGenerator(i: Int) = List.range(1, i + 1)
val resultList = listGenerator(3).flatMap(listGenerator)
println(resultList)
println(resultList.length)
}
}

My assumption is im declaring the numberlist worngly in the line val numberList =. PLease validate and let me know

I don’t see anything wrong in your code. What is the problem you are seeing ?
I am seeing the output List(1, 1, 2, 1, 2, 3)
Why you are not able to mark it complete ? What warnings/errors you are getting ?
Probably the lab assignment might be looking for a single statement like below without additional overhead of defining listGenerator and resultList.

numberList.flatMap(x => List.range(1,x+1))

1 Like

This line should probably use your input instead of a hardcoded 3.

1 Like

Thanks a ton, worked for me without hardcoding the numbers too

Hi,
could you please let me know what changes you have made to the code given here as even I am not able to continue my assignment.

I would recommend you to open a new question, explaining the problem and showing tus what have you tried. And what you do not understand. IMHO, if you are learning giving you just the code won’t help in any way, you have to come with it yourself.

1 Like

Can Someone help me, please
Write the higher order function myHigherOrderFunction which takes two inputs, one function (addOne in this case) and another integer, and gives the output as function(integer) which is an integer.
object HigherOrderFunction{
def main(args :Array[String]){
val input : Int = args(0).toInt
println(myHigherOrderFunction(addOne, input))
def addOne(arg: Int): Int = { arg * 9 }
def myHigherOrderFunction //complete the code
}
}

So you want myHigherOrderFunction to return a function which takes an Int as input? Whas is the return-value of that returned function?

If I understood you correctly, you already have the body of myHigherOrderFunction and the signature shouldn’t be difficult if you know the types.

Try to solve it by yourself, and if you have problems then come back again being clear with what you tried and why it didn’t work.

Working for me

object ListHigherOrderFunction {
def main(args: Array[String]): Unit = {

val input = args(0).toInt
val intList: List[Int] = (1 to input).flatMap((x: Int) => List(x)).toList
def factorial(num: Int): Int = {
  def fac(x: Int, acc: Int): Int = {
    if (x == 1) acc
    else fac(x - 1, acc * x)
  }
  fac(num, 1)
}
def myHigherOrderFunction(fac: (Int) => Int, intList: List[Int]) = intList.foreach((x: Int) => fac(x))
myHigherOrderFunction(factorial, intList)
def listGenerator(i: Int) = List.range(1, i + 1)
val resultList = listGenerator(input).flatMap(listGenerator)
println(resultList)
println(resultList.length)

}

}

Check the path with pwd .

Go to root folder with cd .

This program helps you learn flatMap operation.

In the root folder create a file with name flatMapUsage using the following command: touch flatMapUsage.scala

Open the file with vim editor using the following command: vim flatMapUsage.scala

flatMapUsage takes integer input from command line. A list must be generated with the input number of elements in the list starting from one. Therefore, when input is 3, List(1,2,3) must be generated. Each number in the List generated numberList = List(1,2,3) must be used to create separate lists with elements equal to the number starting from 1. Therefore, when input is 3; List(1), List(1, 2), List(1,2,3) must be created and joined together to give output resultList as List(1, 1, 2, 1, 2, 3) .

Change the file mode to insert by clicking i .

Copy the following flatMapUsage program into the file: object flatMapUsage{ def main(args :Array[String]){ val input :Int = args(0).toInt val numberList = //write your code def listGenerator //write your code val resultList = //write your code println(resultList) println(resultList.length) } } After copying, press Esc followed by :wq to save the file.

Compile the program using:

scalac flatMapUsage.scala

Execute the program to print the outputs:

scala flatMapUsage.scala 3

Output will be: List(1,1,2,1,2,3) 6

CONTINUE I gave this same code as mentioned by scalalearner,but its not proceeding! Can u help me

Actually there is no error in this code, but when I give the same piece of code,it is not proceeding, may i know why! In fact I m getting the same output as mentioned in my lab exercise.
Can u pl check!

object flatMapUsage{
def main(args :Array[String]){
val input :Int = args(0).toInt
val numberList = List.range(1,input)
def listGenerator(i: Int) = List.range(1,i+1)
val resultList = listGenerator(3).flatMap(listGenerator)
println(resultList)
println(resultList.length)
}
}

Output will be:
List(1,1,2,1,2,3)
6

This the exact output i got! CAn someone solve plz!

Would you mind formatting your code using Markdown so that it’s properly indented? It’s hard to read code that isn’t indented, which makes it hard to help you.

Can you describe what you did to debug the code yourself? Where exactly did you get stuck?

Hi…Did you find the solution? if yes, please share

object HigherOrderFunction{
def main(args :Array[String]){
val input : Int = args(0).toInt
println(myHigherOrderFunction(addOne, input))
def addOne(arg: Int): Int = { arg * 9 }
def myHigherOrderFunction (argFn: Int => String, argVal:List[Int]): List[String] = { argVal.map(argFn) }
println(myHigherOrderFunction(addOne, input))

Can you please help correct my code

}
}