【问题标题】:Trying to Pattern match an integer argument "globally" within a function - Scala尝试在函数中“全局”匹配整数参数 - Scala
【发布时间】:2020-06-20 05:01:38
【问题描述】:

首先,如果本文的标题具有误导性,我深表歉意,它基于我认为我的问题可能是什么。我正在创建一个惰性元组列表,最终目标是找到值小于或等于 n 的 所有毕达哥拉斯三元组,而不是创建所有可能的“三元组”组合,我已经这样做了我认为的方法更有效,但可能有点矫枉过正,但它确实有效 :)

  val n = 20
  val increasingTriplets  = unfold(Option(1, 1, 1)) {
    case None => None
    case Some(current) =>
      val maybeNext = current match {
        case (`n`, `n`, `n`) => None
        case (x, `n`, `n`) => Some(x + 1, x + 1, x + 1)
        case (x, y, `n`) => Some(x, y + 1, y + 1)
        case (x, y, z) => Some(x, y, z + 1) // (1,1,1) , (1,1,2), (1,1,3)...etc
      }
      Some(current, maybeNext)
  }.take(nCr(`n`+3-1,3).toInt).filter({ // The formula for the number of combinations with repetition -
    case (a,b,c) => a*a + b*b == c*c    // - allowed of ???? objects from ???? types is  ????+????−1C????
                                       // Ensures minimum number of lists is always generated(I think)
  })    

    println(unfold_with_pythcomp(increasingTriplets,pythcomp).toList)
    // n = 20 List((3,4,5), (4,3,5), (5,12,13), (12,5,13), (6,8,10)
    List((3,4,5), (4,3,5), (5,12,13), (12,5,13), (6,8,10), (8,6,10),
   (8,15,17), (15,8,17), (9,12,15), (12,9,15), (12,16,20), (16,12,20))

    // n = 10
    List((3,4,5), (4,3,5), (6,8,10), (8,6,10))


  // Implementation of unfold

  def unfold[A, S](z: S)(f: S => Option[(A, S)]): LazyList[A] =
    f(z).map((p: (A, S)) => p._1 #:: unfold(p._2)(f)).getOrElse(LazyList.empty[A])

  def unfold_with_pythcomp[A](l1: LazyList[A], f: A => Option[(A, A)]): LazyList[A] =
  // unfolds result list appending the "complement"
    l1 match {
      case LazyList() => l1
      case x #:: y => f(x) match {
        case Some((l, r)) => l #:: r #:: unfold_with_pythcomp(y, f)
      }
    }

  def pythcomp(t: (Int, Int, Int)): Option[((Int, Int, Int), (Int, Int, Int))] = t match {
      //Creates a "complement" to add since (3,4,5) <-> (4,3,5)
    case (a, b, c) => Some((a, b, c), (b, a, c))
  }

  def fact(n: Int): BigInt = { // These are just for efficiency, you may ignore
    @tailrec def go(n: Int, acc: BigInt): BigInt =
      if (n <= 0) acc
      else go(n - 1, BigInt(n) * acc)
    go(n, 1)

  }
  def nCr(n: Int, r: Int): BigInt = { // These are just for efficiency, you may ignore
    fact(n)/(fact(n-r) * fact(r))
  }

现在我正在尝试将其变成一个函数,该函数将执行完全相同的操作,同时仅将 n 作为参数,这可能非常简单,但是我在尝试与给定的 n 参数进行模式匹配时遇到了问题。到目前为止,这是我想出的:

  def unfold_remix[Int](`n`: Int): LazyList[Option[(Int, Int, Int)]] =
  // not sure if I can use backticks here
    unfold(Option(1, 1, 1)) {
      case None => None
      case Some(current) =>
        // Problem could also be from here.
        val maybeNext = current match {
          case (`n`, `n`, `n`) => None
          case (x, `n`, `n`) => Some(x + 1, x + 1, x + 1)
          case (x, y, `n`) => Some(x, y + 1, y + 1)
          case (x, y, z) => Some(x, y, z + 1) // (1,1,1) , (1,1,2), (1, 1,3)...1st
        }
        Some(current, **maybeNext**)
      // I get an error on this line under maybeNext.
      // Too many arguments for method apply(A)
    }

我在尝试初始化列表时不断收到错误,就像我之前所做的那样,作为 Scala 的初学者,我似乎无法理解为什么。我已经尝试过使用和不使用反引号的论点,但这也不起作用。想知道是否有人可以帮助我解决问题,并且我想知道是否可以在函数中“全局”匹配给定的整数参数。 谢谢!

【问题讨论】:

  • 你见过this吗?
  • 是的,谢谢@jwvh。我的帖子的目的是尽可能发现我的方法中的错误来源。

标签: scala functional-programming unfold


【解决方案1】:

不知道这是否返回正确的结果,但它可以编译(类型检查)。

def unfold_remix(n: Int): LazyList[Option[(Int, Int, Int)]] =
  unfold(Option(1, 1, 1)) {
    case None => None
    case Some(current) =>
      val maybeNext = current match {
        case (`n`, `n`, `n`) => None
        case (x, `n`, `n`) => Some(x + 1, x + 1, x + 1)
        case (x, y, `n`) => Some(x, y + 1, y + 1)
        case (x, y, z) => Some(x, y, z + 1)
      }
      Some((Some(current), maybeNext))
  }

(而且,是的,对于一个相对简单的任务来说,这实在是太多代码了。)

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 2012-09-23
    • 2020-04-08
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多