bsky
bsky

Reputation: 20222

Scala - error when passing parameters

I am doing the Martin Odersky course about Scala. In one of the assignments I have the following type:

type Occurrences = List[(Char, Int)]

I have defined a method which subtracts an element of type (Char, Int) from an element of type Occurrences.

  def subtractOne(x: Occurrences, (char: Char, nr: Int)): Occurrences = x match {
    case List() => throw new Exception("can not subtract")
    case (char, nr2) :: ocs => {
      if(nr2 > nr) (char, nr2 - nr) :: ocs
      else if(nr2 == nr) ocs
      else throw new Exception("can not subtract")
    }
    case _ :: ocs => subtractOne(ocs, (char, nr))
  }

However, I am getting some unclear errors on the first line: Wrong parameter and Definition or declaration expected.

Is there anything wrong with the way I declared the parameters?

Upvotes: 0

Views: 704

Answers (2)

freakybit
freakybit

Reputation: 59

Tuples are defined under one name - charAndNr: (Char, Int) Also Nil is preferred to List()

Upvotes: 2

vvg
vvg

Reputation: 6385

Do not use brackets in parameter list. Unless you want to define tuple but it should be done with one name.

def subtractOne(x: Occurrences, char: Char, nr: Int): Occurrences = x match {

Upvotes: 2

Related Questions