Scala模式匹配错误,“错误的简单模式:错误使用_ *(不允许序列模式)”

问题描述 投票:1回答:1

我正在编写Coursera的作业,我遇到了有关Scala模式匹配的问题。

“Scala编程”一书有以下代码:

expr match { 
    case List(0, _ * ) => println("found it") 
    case _ => 
}

所以我写了一个类似的代码来计算列表中每个字符的频率:

/**
 * This function computes for each unique character in the list `chars` the number of
 * times it occurs. For example, the invocation
 *
 *   times(List('a', 'b', 'a'))
 *
 * should return the following (the order of the resulting list is not important):
 *
 *   List(('a', 2), ('b', 1)) */
 def times(chars: List[Char]): List[(Char, Int)] = {
   def addTime(char: Char, times: List[(Char, Int)]):List[(Char, Int)] = times match {
     case List(head @ (_*), (c , times), tail @ (_*)) if c == char => List(head, (c, times + 1), tail)
     case _ => List((char, 1))
 }

   if(chars.isEmpty) Nil else addTime(chars.head, times(chars.tail))
}

但是,编译器抱怨:

Error:(81, 29) bad simple pattern: bad use of _* (sequence pattern not allowed)
    case List(head @ (_*), (c , times), tail @ (_*)) if c == char => List(head, (c, times + 1), tail)

虽然我以另一种方式成功实现了这个方法,但是在2个辅助函数的帮助下,我不知道为什么不允许这个序列模式。我试过谷歌,但我找不到答案。

任何建议将不胜感激。提前致谢。

scala pattern-matching
1个回答
3
投票

任何模式匹配仅以最多一种方式分解原始值。如果case List(head @ (_*), (c , times), tail @ (_*))是合法的,它将允许headctimestail的许多选项。你的意图似乎是按顺序尝试所有这些方法,并且当守卫c == char变为真时匹配,但这不是Scala模式匹配的工作方式。

事实上,为了使规则简单,_*只允许在最后。像List(_*, c)这样的东西可以合理地允许,但事实并非如此。

© www.soinside.com 2019 - 2024. All rights reserved.