Need help in scala

hi All,

Please help me in the scala code. i am new to scala with no java background.
The code is not giving any compilation error. However its not giving output also.
Not sure what am missing in my code. Please help

Question is

The Expression abstract class is created with 4 other case classes. Write a function which takes an Expresssion object as input, and use match so that when an Expression object is taken:

  1. It is a number is printed for Number object.
  2. It is a string expression is printed for Var object.
  3. It is a unary operation is printed for UnOp object.
  4. It is a binary operation is printed for BinOp object.
  5. Invalid Expression is printed in all other cases.

my code is

abstract class Expression
case class Var(name: String) extends Expression
case class Number(num: Double) extends Expression
case class UnOp(operator: String, arg: Expression) extends Expression
case class BinOp(operator: String, left: Expression, right: Expression) extends Expression
object CaseClassMatching extends App {
val op = BinOp("+", Number(1), Number(4))
describe(op)
def describe (i: Expression): String = i match {
case Var() => “Its is string expression”
case Number(
) => " It is a number"
case UnOp(,) => " it is a unary operation"
case BinOp(,,_) => " it is a binary operation"
}
}

Hi, welcome to the community.

For future posts, you can format your code like this:

```scala
// your code here.
```

Now, you already basically solve your problem.
The only thing is a that you are not printing the answer.

Just do this

println(describe(op))

Happy coding.

Thank you very much Sir. it worked perfectly and came to know what i am missing.

@dhilip - can you please post the working code as somehow the code which I tried similar to what you have mentioned above is not working? thank you.

scalac CaseClass.scala
CaseClass.scala:12: error: illegal start of simple pattern
case UnOp(,) => “It is a unary operation”
^
CaseClass.scala:12: error: illegal start of simple pattern
case UnOp(,) => “It is a unary operation”
^
CaseClass.scala:13: error: illegal start of simple pattern
case BinOp(,) => “It is a binary operation”
^
CaseClass.scala:13: error: illegal start of simple pattern
case BinOp(,
) => “It is a binary operation”

1 Like

‘’‘scala
abstract class Expression
case class Var(name: String) extends Expression
case class Number(num: Double) extends Expression
case class UnOp(operator: String, arg: Expression) extends Expression
case class BinOp(operator: String, left: Expression, right: Expression) extends Expression
object CaseClassMatching extends App {
val op = BinOp("+", Number(1), Number(4))
describe(op)
def describe (i: Expression): String = i match {
case Var(i: String) => “Its is string expression”
case Number(i: Double ) => " It is a number"
case UnOp(i: String, arg: Expression) => “It is a unary operation”
case BinOp( ,,_) => “It is a binary operation”
}
}
‘’’
It is not showing any error, plz correct my code, where i went wrong

If I replace case BinOp(,,_) with case BinOp(_,_,_) and change describe(op) to println(describe(op)), it seems to work.

2 Likes

Complementing @SethTisue answer, you can see it running here.

2 Likes

`abstract class Expression
object ForComprehensions2 extends App {
val input : Int = args(0).toInt

list = List (i <- 1 to input)
def add(number : Int) { for (i <- list to input)
yield i+5 }
for (i <- 1 to input)
println("value: " add(i)

case class Person(val name :String, val age : Int){}
val personList :List[Person] = List(Person(“Robert”, 56), Person(“Chris”, 48), Person(“Benedict”, 45), Person(“Peter”, 47))

for (i<- 1 to 4)
println(personList(i))
}`

here i am getting this error ’ ‘)’ expected but ‘<-’ found. ’ …is there any syntax error or what which i couldn’t understand

This isn’t valid syntax, the arrow <- is only allowed in a for comprehension.

The result of 1 to input is a Range object. Search the documentation for that class, and you will find a way to make a List from that.

1 Like