我想在Scala中编译简单的helloworld, 并获得错误“scala:value capitalize is not char of member” 为什么编译器认为newW是Char?Scala:映射字符串时出现错误的类型推断?
val dict = Map(
"hello" -> "olleh",
"world" -> "dlrow"
)
def translate(input: String): String = {
input.split("""\s+""").map(w => dict.getOrElse(w.toLowerCase, w).map(newW =>
(if (w(0).isUpper) newW.capitalize else newW))
).mkString(" ")
}
从技术上讲,不像Haskell,'String'不是'List [Char]'。我认为他们之间有一个隐含的转换。 – 2013-02-14 23:53:20
你说得对,我已经相应地更新了文字。 – 2013-02-14 23:55:46