传递 Set[List[Int]] 类型值而不是 List[Int] - 为什么会这样? [英] Passing a Set[List[Int]] typed value instead of List[Int] - why does that work?

查看:25
本文介绍了传递 Set[List[Int]] 类型值而不是 List[Int] - 为什么会这样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我查看签名和函数调用时,我很难理解以下内容.

I have difficulties to understand the following when I look at the signatures and the function calls.

在我的工作表中,我有以下内容(摘自 Coursera 讲座):

In my worksheet I have the following (taken from a Coursera lecture):

 object nqueens {

  def queens(n: Int) : Set[List[Int]] = {
    def placeQueens(k: Int) : Set[List[Int]] =
      if (k == 0) Set(List())
      else
      for  {
        queens <- placeQueens(k - 1)
        col <- 0 until n
        if isSafe(col, queens)
      } yield col :: queens
    placeQueens(n)
  }

  def isSafe(col: Int, queens: List[Int]) : Boolean = {
    val row = queens.length
    val queensWithRow = (row - 1 to 0 by -1) zip queens
    queensWithRow forall {
      case (r, c) => col != c && math.abs(col - c) != row -r
    }
  }

  def show(queens: List[Int]) = {
    val lines =
      for (col <- queens.reverse)
        yield Vector.fill(queens.length)("* ").updated(col, "X ").mkString
    "\n" + (lines mkString "\n")
  }

  (queens(4) map show) mkString "\n"

}

考虑到 placeQueenisSafe 的签名:

Considering the signatures of placeQueen and isSafe:

def placeQueens(k: Int) : Set[List[Int]]

def isSafe(col: Int, queens: List[Int]) : Boolean

我想知道它为什么有效.我们调用 placeQueens 并将结果保存在 queens(在 for 循环中).

I wonder why it works. We call placeQueens and save the result in queens (in the for loop).

结果应该是 Set[List[Int]] 类型.然后我们用 IntSet[List[Int]] 两个参数调用 isSafe - 但我不明白为什么会这样,因为 queens 应该是 Set[List[Int]] 类型,isSafe 的参数应该 List[Int].

The result should be of type Set[List[Int]]. Then we call isSafe with two parameters of Int and Set[List[Int]] - but I don't see why that works because queens should be of type Set[List[Int]] and the parameter for isSafe should List[Int].

推荐答案

我们调用 placeQueens 并将结果保存在 Queens 中

We call placeQueens and save the result in queens

您的代码没有将 placeQueens 的结果保存到 queens 中.

Your code is not saving the result of placeQueens into queens.

for  {
    queens <- placeQueens(k - 1)
    col <- 0 until n
    if isSafe(col, queens)
  } yield col :: queens

这段代码实际上用于理解.有问题的特定行:

This code is actually using for comprehension. The particular line in question:

queens <- placeQueens(k-1) 

正在将 List[Int] 存储到皇后中,因为它正在迭代从 placeQueens 返回的 Set[List[Int]].举一个更简单的例子来帮助说明发生了什么,请考虑:

is storing a List[Int] into queens, since it is iterating over the Set[List[Int]] returned from placeQueens. To give a simpler example that might help illustrate what's going on, consider:

val a = Set(1,2,3)
val b = for (x <- a) yield x + 2

执行此代码后,b 将是 Set(3,4,5).这是因为在 for 循环中的每次迭代中,x 首先是 1,然后是 2,然后是 3.

After executing this code, b will be Set(3,4,5). This is because at each iteration in the for loop, x is first 1, then 2, then 3.

这篇关于传递 Set[List[Int]] 类型值而不是 List[Int] - 为什么会这样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆