【发布时间】:2013-11-21 10:57:47
【问题描述】:
我正在尝试在列表列表中查找一个元素;特别是,如果可以在一个国家/地区找到特定城市。
我有州:List[State] 和城市:List[City],这意味着国家/地区表示为List[List[City]]。
我写了这段代码,但似乎我遇到了问题。这是sn-p:
case class city (
name: String,
CodePostal: Double,
visit: Boolean
)
def belongToCountry(p: city): Boolean =
countries.flatten.foreach {
case p => return true
case _ => return false
}
def belongToCountry(p: city): Boolean =
countries.foreach(s => s.city.contains(p))
【问题讨论】:
-
应该是
country而不是countries? -
我认为函数的签名应该是
def belongToCountry(country: List[List[City]], p: City): Boolean
标签: algorithm scala functional-programming