Yes, this is an abuse of the match. You basically just wrote a big if-else-if block, but in a more awkward form. What is wrong with if statements?
I think it's much cleaner to just write this:
def countChange(money: Int, coins: List[Int]): Int = { if(money == 0) 1 else if (money < 0) 0 else if (coins.isEmpty) 0 else countChange(money, coins.tail) + countChange(money - coins.head, coins) }
If you want to stick with match , you can move most of the check into a match, so that it really does something:
def countChange(money: Int, coins: List[Int]): Int = { (money, coins) match { case (0, _) => 1 case _ if (money < 0) => 0 case (_, Nil) => 0 case (_, coinsHead :: coinsTail) => countChange(money, coinsTail) + countChange(money - coinsHead, coins) } }
source share