0

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?

1
  • 1
    You cannot "pattern match" a tuple in the parameter list of a method. Use def subtractOne(x: Occurrences, e: (Char,Int)): Occurrences then refer your tuple elements via e._1 and e._2 Commented Dec 12, 2015 at 15:35

2 Answers 2

2

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 {

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.