Code - please assist to solve the error ( its also done)

Declare an implicit integer variable 5. Generate a List with input1 such that the number of elements is equal to input1, and the numbers are from 1 to input1 (when input is 3 -> List(1,2,3)). Write a curried function add which takes two integer variables in which one variable is an implicit variable. Using a for comprehension, apply the add function on the generated list and print the result.

Following is the code that I have done…

object Demo extends App{
val input1:Int = args(0).toInt
implicit val num = 5
val list = List.tabulate(input1)(n=>n+1)
def add(implicit x:Int)(y:List[Int]):Int={
val result = for(i<- list){x+i}
println(result)
}
add(x,list)

}

I am getting the following error…

Demo.scala:5: error: ‘=’ expected but ‘(’ found.
def add(implicit x:Int)(y:List[Int]):Int = {
^
Demo.scala:10: error: illegal start of simple expression
add(x,list)
^
two errors found

The specific error is that implicit arguments should come last, after the regular arguments. Also, please enclose code in fenced code blocks.

regards,
Siddhartha

I have modified again:

object Demo extends App{
val input1:Int = args(0).toInt
implicit val num = 5
val list = List.tabulate(input1)(n=>n+1)
def add(y:List[Int])(implicit x:Int):Int={
val result = for(i<- list){x+i}
println(result)
}
add(list)(x)

}

I have got the following error …

Demo.scala:7: error: type mismatch;
found : Unit
required: Int
println(result)
^
Demo.scala:9: error: not found: value x
add(list)(x)
^
two errors found

For your first error:
The last line of the method definition is the return value. In your definition of add, that is println(result) which is of type Unit and the return type is specified as Int. You probably just want result.
For your second error:
There is no x in scope when you are calling add(list)(x). I think your intention is add(list)(num) but also you don’t need to explicitly pass the implicit argument if you have an implicit argument in scope. So if you call add(list) there (omitting the second argument), the compiler will find and add the implicit Int that you’ve declared and pass it automatically.

1 Like

I think I have reaching closer to the final step.I have done the modification and got the following output:

…code
object Demo extends App{
val input1:Int = args(0).toInt
implicit val num = 5
val list = List.tabulate(input1)(n=>n+1)
def add(y:List[Int])(implicit x:Int):Unit={
val result = for(i<- list){x+i}
println(result)
}
add(list)(num)

}

…output

$ scala Demo 3
()

but the output will be:
6
7
8

Please aasist why the blank braces are coming ?

Thank you all.Its also done :smiley:

Hello… still I am getting the blank output()
Please let me know successful executed code Thank you.

Can you please help … output is blank
object Demo extends App{
val input1:Int = args(0).toInt
implicit val num = 5
val list = List.tabulate(input1)(n=>n+1)
def add(y:List[Int])(implicit x:Int):Unit={
val result = for(i<- list){x+i}
println(result)
}
add(list)
}

Without yield, for just returns Unit (which is akin to void in C and Java). Try using for with yield.