0

I'm working on the first assignment of the Coursera class.

For the following code, I'm stuck on the compile-time errors.

    object pascal {
        def main(c: Int, r: Int) = {
                pascal(c, r)
            }
            def pascal(c: Int, r: Int) = {
                (c, r) match {
                    case ((r < 0) || (c < 0)) => throw new Exception
                                                 ("r and c must be > 0")
                    case r == 0 => 1
                    case r == 1 => 1
                    case c == 0 => 1
                    case r == c => 1
                    case _ => pascal(r-1,c-1) + pascal(r-1,c-1)
            }
        }
     }

vagrant@precise64:/vagrant/Workspace/Scala/hw1$ scalac pascal.scala
pascal.scala:7: error: not found: value ||
                        case ((r < 0) || (c < 0)) => throw ...
                                      ^
pascal.scala:8: error: value == is not a case class constructor, nor 
does it have an unapply/unapplySeq method
                        case r == 0 => 1

Please advise.

Thanks.

Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384

1 Answers1

3

This is invalid syntax in scala: you need to use so called guards to check for boolean properties:

def pascal(c: Int, r: Int) = {
   (c, r) match {
     case (c,r) if ((r < 0) || (c < 0)) => throw new Exception
                                                 ("r and c must be > 0")
     ...
}

By the way much more idiomatic to write code like this:

def pascal(c: Int, r: Int) = {
   require((r >= 0) && (c >= 0), "r and c must be >= 0")
   (c, r) match {
     case (_, 1) => 1
     ...
}

And you have to change error message to "r and c must be >= 0"

Community
  • 1
  • 1
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228