Scala中的递归程序无法运行

时间:2014-05-22 21:23:05

标签: list scala recursion

我正在尝试使用递归在Scala中编写一个程序,以查看两个列表是否不相交。这是我第一次在Scala中编写程序。

object Main {
    def member(x:Int,lst:List[Int]):Boolean= {
        if (lst == Nil)
            return false
        else
            if (x != lst.head)
                return member(x, lst.tail)
            else
                return true
    }

    def disjoint(s1:List[Int], s2:List[Int]):Boolean= {
        if (s1 == Nil)
            return true
        else
            if (member(s1.head,s2))
                return false
            else
                return disjoint(s1.tail, s2)
    }
     def main(args: Array[String]) {
        val lst1 = 2 :: (6 :: (1 :: Nil))
        val lst2 = 1 :: (3 :: (5 :: (7 :: (9 :: (11 :: Nil)))))
        val lst3 = disjoint(lst1, lst2)
     }
}

它编译但不运行。非常感谢所有帮助。

2 个答案:

答案 0 :(得分:2)

这可能就是你要找的东西:

def areDisjoint[T](s1: Seq[T], s2: Seq[T]): Boolean =
  (s1.toSet & s2.toSet).isEmpty

val lst1 = List(2, 6, 1)
val lst1a = List(2, 6)
val lst2 = List(1, 3, 5, 7, 9, 11)
println(areDisjoint(lst1, lst2))
println(areDisjoint(lst1a, lst2))

=>

false
true

诀窍是使用Sets上的&运算符为您完成工作。 Set使用哈希来有效地测试给定对象是否是成员。

答案 1 :(得分:2)

如果你真的想使用递归只是为了看看如何递减Scala中的列表,请按照以下方式进行:

def isMember(x: Int, l: List[Int]): Boolean =
  l match {
    case Nil => false
    case head :: tail => x == head || isMember(x, tail)
  }

def areDisjoint(l1: List[Int], l2: List[Int]): Boolean =
  l1 match {
    case Nil => true
    case head :: tail => !isMember(head, l2) && areDisjoint(tail, l2)
  }

match执行"pattern-matching",它提供了一种语法上简单的方法来从List中提取头部和尾部。请注意,这在实践中确实不是很好的Scala代码,原因有三:

  1. 它进行线性搜索并在O(n ^ 2)时间内运行。 Set版本运行得更快,至少在大型列表上运行。

  2. List已有内置.contains方法(请参阅here),因此您根本不需要撰写isMember。< / p>

  3. 仅限于List[Int]工作。 Seq[T]版本适用于任何类型的序列,包含任何类型的项目。