Need help in Lab test

We had written code as

object ForComprehensions1 extends App {
 val input : Int = args(0).toInt
 val list : Int = List(1:input)
for (i <- list if i % 2 == 0)
yeild i
println("even :" i)



 val stringList :List[String] = List("Scala", "Ruby", "Python", "C#", "Java", "Groovy", "JavaScript", "PHP", "Haskell")
 for {
if (strigList startWith "P")
print(stringList)
} yield stringList
}

we are getting below error as

$ scalac ForComprehensions1.scala
ForComprehensions1.scala:3: error: not found: type input
val list : Int = List(1:input)
^
ForComprehensions1.scala:4: error: value filter is not a member of Int
for (i <- list if i % 2 == 0)
^
ForComprehensions1.scala:6: error: value i is not a member of String
println(“even :” i)
^
three errors found

ForComprehensions1.scala:10: error: illegal start of simple pattern
if (stringList startsWith “P”)
^
ForComprehensions1.scala:11: error: ‘<-’ expected but ‘;’ found.
print(stringList)
^
ForComprehensions1.scala:12: error: ‘<-’ expected but ‘}’ found.
} yield stringList
^
three errors found

We need output as

even : 2
Python
PHP

Please let us know where we are wrong

Firstly, List(1:input) is not valid scala syntax. What you need is (1 to input).toList

You have also misspelled yield. And you clearly don’t mean if (stringList startsWith “P”) - you want entries of the list to start with “P”. The correct code begins with:

for {string <- stringList
      if (string startsWith “P”)
      ...
} ...

We need to write code as

object ForComprehensions1 extends App {
 val input : Int = args(0).toInt
 val list = (1 to input).toList
for (i <- list if i % 2 == 0)
yeild i
println("even :" i)
 val stringList :List[String] = List("Scala", "Ruby", "Python", "C#", "Java", "Groovy", "JavaScript", "PHP", "Haskell")
 for {string <- stringList
if (string startWith "P")
} yield string
println(string)
}

error as

$ scalac ForComprehensions1.scala
ForComprehensions1.scala:6: error: value i is not a member of String
println(“even :” i)
^
ForComprehensions1.scala:10: error: value startWith is not a member of String
if (string startWith “P”)
^
ForComprehensions1.scala:12: error: not found: value string
println(string)
^
three errors found

It is startsWith not startWith, should be println("even :" + i) (or better println("even :" + i.toString) or use string interpolation). And stringis a local variable inside thefor`, you should define a list variable and use it.

Now we written code as

object ForComprehensions1 extends App {
 val input : Int = args(0).toInt
 val list = (1 to input).toList
for (i <- list if i % 2 == 0)
yield i
println("even :" + i.toString) 


 val stringList :List[String] = List("Scala", "Ruby", "Python", "C#", "Java", "Groovy", "JavaScript", "PHP", "Haskell")
 for {string <- stringList
if (string starstWith "P")
println(string)
} yield string
}

error as

$ scalac ForComprehensions1.scala
ForComprehensions1.scala:6: error: not found: value i
println(“even :” + i.toString)

ForComprehensions1.scala:12: error: ‘<-’ expected but ‘}’ found.
} yield string
^
ForComprehensions1.scala:15: error: Unmatched closing brace ‘}’ ignored here
}
^

New codeas

object ForComprehensions1 extends App {
 val input : Int = args(0).toInt
 val list = (1 to input).toList
for (i <- list if i % 2 == 0)
yield i
println("even :" + i.toString) 


 val stringList :List[String] = List("Scala", "Ruby", "Python", "C#", "Java", "Groovy", "JavaScript", "PHP", "Haskell")
 for {
string <- stringList
if (string startsWith "P")
} yield string

}

error as

$ scalac ForComprehensions1.scala
ForComprehensions1.scala:6: error: not found: value i
println(“even :” + i.toString)
^
one error found

Below code is fine for

object ForComprehensions1 extends App {
 val input : Int = args(0).toInt
 val list = (1 to input).toList
for (i <- list if i % 2 == 0)
yield i



 val stringList :List[String] = List("Scala", "Ruby", "Python", "C#", "Java", "Groovy", "JavaScript", "PHP", "Haskell")
 for {
string <- stringList
if (string startsWith "P")
} yield string

}

It is working fine but there is no output, we need output

Output will be:
even : 2
Python
PHP

Another issue in same context

object Demo extends App {
 val input1 :Int = args(0).toInt
 val list  = (1 to input1).toList

for (i <- list)

    yield i+5
 }

Not able to print value

We need out as

6
7
8

code working fine but no print

It isn’t printing any values because…you never ask it to?

If you want to print i + 5, maybe you should try println(i + 5) instead of yield i + 5. Yield returns the result.

val lengths = for (i <- list) yield i + 5

You might want to consult a book to familiarize yourself with the basics of Scala syntax. It’s going to be very painful to try to get your advice bit by bit on a forum! There are a bunch of good ones…Scala for the Impatient is, as the name suggests, pretty good if you want to get going in a hurry.

Thanks for ur advice , we are able to get output but could not pass Lab test. Our task is

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.

We written code as

object Demo extends App {
 val input1 :Int = args(0).toInt
 val list  = (1 to input1).toList

for (i <- list)

    println(i + 5)
 }

It is not helping us, please suggest.

Working code is:
object ForComprehensions1 extends App {
val input : Int = args(0).toInt
val list = (1 to input).toList
for (i <- list if i % 2 == 0)
println("even: " + i)
val stringList :List[String] = List(“Scala”, “Ruby”, “Python”, “C#”, “Java”, “Groovy”, “JavaScript”, “PHP”, “Haskell”)
for {
string <- stringList
if (string startsWith “P”)
} println(string)
}

@ashishsme14 - You should talk to your teaching assistant or whoever else can help you with the class, if there is such a person available. Reading a solution with full code here is only going to impair your understanding. Programming is a skill–you can’t build it effectively by having others give you the answers, just like you can’t much get stronger by watching others lift weights, or better at darts by watching people play.

Figure out what you don’t know, and ask targeted questions here…or talk to someone teaching the class who might have a better idea of what you’re expected to know and what you’re having trouble with. And then do it yourself! And understand what you’re doing.

For instance, look at the requirements:

  1. Declare an implicit variable 5. You’re not doing this. Do you know what an implicit variable is? Do you know how to declare one?

  2. Generate a list with input1 such that the number of elements is equal to input1 and the numbers are from 1 to input1. You are doing this–a previous answer told you how. Do you understand why it works?

  3. Write a curried function add which takes two integer variables in which one variable is an implicit variable. Do you know how to create a function (or method, actually, in this case)? Do you know how to specify variables? Do you know how to make one implicit? Do you know what they mean by “curried function”?

  4. Using a for comprehension, apply the add function on the generated list and print the result. You have the for comprehension, but you’re using + instead of an add function. You’re also printing.

Anyway, this assignment requires you to have basic familiarity with a range of concepts in Scala, and if you happen to see code that solves the assignment but you don’t get the concepts, you are not accomplishing the learning you’re supposed to!

If you don’t understand the individual concepts, go back and study them until you do!

1 Like

We had modify code as

    object Demo extends App {
 val input1 :Int = args(0).toInt
 val list  = (1 to input1).toList
implicit val test = 5
def add(x : Int)(implicit y: Int = 5) = x + y
for (i <- list)
    println(i + 5)
 }

it is working fine for us thanks

@ashishsme14 - You missed the part where you use the add method in the for comprehension–instead of i + 5 you should use the add method. (What argument(s) should you pass to add? How would it be different if test were not implicit, and add did not take an implicit parameter?)