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:
It is a number is printed for Number object.
It is a string expression is printed for Var object.
It is a unary operation is printed for UnOp object.
It is a binary operation is printed for BinOp object.
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"
}
}
@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â
âââ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
`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