我对 scala 和编程很陌生(只是为了好玩),我试图理解尾递归和集合,但调试真的很难。
我有 2 个清单:
val quoters = List[Map[String,List[String]]]
val quoted = List[Map[String,List[String]]]
前任 :
val quoters = List(Map("author1"->List("1","7","8")),Map("author2"->List("2","4","6","3")),Map("author3"->List("5","2","1","3")))
val quoted = List(Map("author1"->List("5","6")),Map("author2"->List("5","8","1")),Map("author3"->List("4")))
"quoters" 引用 "quoted" 和 "quoted" 也引用 "quoters"。
在示例中,:
author1 quoted author2 with "1" and "8",
author2 quoted author3 with "4",
author3 quoted author1 with "5" & author2 with "5" + "1"
我想找到引用“引用”和引用“引用者”的“引用者”圈子......
输出应该是这样的:
val quotesCircle = List(
Map("quoter"->"author1","receiver"->"author2","quote"->"4"),
Map("quoter"->"author2","receiver"->"author3","quote"->"2"),
Map("quoter"->"author3","receiver"->"author1","quote"->"1")
)
我的问题:
1/我认为我在滥用集合(Json 似乎太多了......)
2/我可以只与列表列表相交:
def getintersect(q1:List[List[String]],q2:List[List[String]])={
for(x<-q1;r<-q2; if (x intersect r) != Nil)yield x intersect r
}
但不是地图列表的结构。
3/我为递归尝试了这个,但它不起作用,因为......好吧我真的不知道:
def getintersect(q1:List[List[String]],q2:List[List[String]])= {
def getQuotedFromIntersect(quoteMatching:List[String],quoted:List[List[String]]):List[List[String]]={
for(x<-q1;r<-q2; if (x intersect r) != Nil)
getQuotedFromIntersect(x intersect r,quoted)
}
}
我希望我足够清楚:/
先感谢您 !
菲利克斯