0

我必须用语法解析一个字符串

lazy val expr: Parser[Term ~ Option[<recursion>]] = term ~ opt(expr)

你看,它使用自己来解析子表达式。但是这种类型的结果是什么?为了重现该问题,我创建了一个类似的问题

def quine(depth: Int, result: (Any, Option[Any])): (Any, Option[Any]) = 
    if (depth == 0) result else {quine(depth - 1, (depth, Some(result)))}
quine(5, (true, None))

如您所见,我使用Any了因为我不知道如何给出确切的类型。

列表以某种方式定义了 proto 类型List[A],扩展了Cons[A](head: A, tail: List[A])一些魔法Nil。这允许列表以某种方式重现。但是,在我的情况下我该怎么办?

4

1 回答 1

0

我不确定您在这里要做什么,因为您的result类型在函数定义和调用之间是不同的(第一个元素Int在前者和Boolean后者中,这是行不通的)。

但是,如果这只是一个错字,这样的事情应该可以解决问题:

 case class Quine(n: Int, q: Option[Quine])
 def quine(depth: Int, result: Quine): Quine =  
  if (depth == 0) result else 
    quine(depth - 1, Quine(depth, Some(result)))

 quine(5, Quine(6, None))
于 2016-06-24T16:38:36.577 回答