Recursion 递归遍历树并返回所有找到值的非静音列表?可能的

Recursion 递归遍历树并返回所有找到值的非静音列表?可能的,recursion,f#,tree-traversal,Recursion,F#,Tree Traversal,我想知道这是否有可能,正如问题所暗示的那样。 我的问题是,我似乎无法理解如何处理一个给定的输入值可以有多个子项这一事实。使用可变SortedSet变量可以轻松解决此问题,如下所示。但我真的很想知道这是否是一个可以用纯递归和创建新的非静音列表或类似方法解决的问题。我希望我的问题是清楚的。恐怕我不知道这是不可能的简单结论。 正如您在下面看到的,if(true)将返回一个列表,而else将返回一个列表列表。因此,下面的代码未处于工作状态 let someSet = new System.Collect

我想知道这是否有可能,正如问题所暗示的那样。 我的问题是,我似乎无法理解如何处理一个给定的输入值可以有多个子项这一事实。使用可变SortedSet变量可以轻松解决此问题,如下所示。但我真的很想知道这是否是一个可以用纯递归和创建新的非静音列表或类似方法解决的问题。我希望我的问题是清楚的。恐怕我不知道这是不可能的简单结论。 正如您在下面看到的,if(true)将返回一个列表,而else将返回一个列表列表。因此,下面的代码未处于工作状态

let someSet = new System.Collections.Generic.SortedSet<string>()
let rec children(value:string,listSoFar) =
    printfn "ID: %A" value 
    someSet.Add(value) works fine of course.
    let newList = List.append listSoFar [value]
    if(not (hasChildren(value))) then 
        newList
    else
        let tmpCollection = database.GetCollection<Collection>("Collection")
        let tmpQuery = Query.EQ("Field",BsonValue.Create(value))
        let tmpRes = tmpCollection.Find(tmpQuery)
        [ for child in tmpRes do
            yield children(child.Value,newList) ]


let resultList = children("aParentStartValue",[]) 
//Or do i need to use someSet values?
let someSet=new System.Collections.Generic.SortedSet()
let rec子项(值:string,listSoFar)=
printfn“ID:%A”值
当然,增加(价值)效果很好。
让newList=List.append listSoFar[value]
如果(不是(hasChildren(value)),那么
新名单
其他的
让tmpCollection=database.GetCollection(“Collection”)
让tmpQuery=Query.EQ(“字段”,BsonValue.Create(value))
让tmpRes=tmpCollection.Find(tmpQuery)
[适用于tmpRes do中的儿童
产生子项(子项值,新列表)]
让resultList=children(“aParentStartValue”,[])
//或者我需要使用一些设置值吗?

除非树嵌套非常深(在这种情况下,这将是低效的),否则可以将代码编写为递归F#序列表达式,使用
yield
yield生成元素

let rec children (value:string) = seq {
    // Produce the current value as the next element of the sequence
    yield value
    if hasChildren value then 
      // If it has children, then get all the children
      let tmpCollection = database.GetCollection<Collection>("Collection")
      let tmpQuery = Query.EQ("Field",BsonValue.Create(value))
      let tmpRes = tmpCollection.Find(tmpQuery)
      // For each child, generate all its sub-children recursively
      // and return all such elements as part of this sequence using 'yield!'
      for child in tmpRes do
        yield! children child.Value }

// Using 'List.ofSeq' to fully evaluate the lazy sequence
let resultList = List.ofSeq (children "aParentStartValue")
let rec children(值:string)=seq{
//生成当前值作为序列的下一个元素
收益率
如果有孩子,那么
//如果它有孩子,那么就把所有的孩子都找来
让tmpCollection=database.GetCollection(“Collection”)
让tmpQuery=Query.EQ(“字段”,BsonValue.Create(value))
让tmpRes=tmpCollection.Find(tmpQuery)
//对于每个子级,递归生成其所有子级
//并使用“yield!”将所有这些元素作为该序列的一部分返回
适用于tmpRes do中的儿童
yield!children.Value}
//使用'List.ofSeq'完全评估延迟序列
让resultList=List.ofSeq(子项“aParentStartValue”)

如果树嵌套得更深,那么情况就更困难了。迭代所有子级时,需要将迄今收集的列表传递给第一个子级,获得结果,然后将结果列表传递给下一个子级(使用类似于
list.fold的方法)。但是上面的代码是干净的,在大多数情况下都可以使用。

除非树嵌套得很深(在这种情况下,这将是低效的),否则您可以将代码编写为递归F#序列表达式,使用
yield
yield>生成元素

let rec children (value:string) = seq {
    // Produce the current value as the next element of the sequence
    yield value
    if hasChildren value then 
      // If it has children, then get all the children
      let tmpCollection = database.GetCollection<Collection>("Collection")
      let tmpQuery = Query.EQ("Field",BsonValue.Create(value))
      let tmpRes = tmpCollection.Find(tmpQuery)
      // For each child, generate all its sub-children recursively
      // and return all such elements as part of this sequence using 'yield!'
      for child in tmpRes do
        yield! children child.Value }

// Using 'List.ofSeq' to fully evaluate the lazy sequence
let resultList = List.ofSeq (children "aParentStartValue")
let rec children(值:string)=seq{
//生成当前值作为序列的下一个元素
收益率
如果有孩子,那么
//如果它有孩子,那么就把所有的孩子都找来
让tmpCollection=database.GetCollection(“Collection”)
让tmpQuery=Query.EQ(“字段”,BsonValue.Create(value))
让tmpRes=tmpCollection.Find(tmpQuery)
//对于每个子级,递归生成其所有子级
//并使用“yield!”将所有这些元素作为该序列的一部分返回
适用于tmpRes do中的儿童
yield!children.Value}
//使用'List.ofSeq'完全评估延迟序列
让resultList=List.ofSeq(子项“aParentStartValue”)

如果树嵌套得更深,那么情况就更困难了。迭代所有子级时,需要将迄今收集的列表传递给第一个子级,获得结果,然后将结果列表传递给下一个子级(使用类似于
list.fold的方法)。但是上面的代码是干净的,在大多数情况下都可以使用。

除非树嵌套得很深(在这种情况下,这将是低效的),否则您可以将代码编写为递归F#序列表达式,使用
yield
yield>生成元素

let rec children (value:string) = seq {
    // Produce the current value as the next element of the sequence
    yield value
    if hasChildren value then 
      // If it has children, then get all the children
      let tmpCollection = database.GetCollection<Collection>("Collection")
      let tmpQuery = Query.EQ("Field",BsonValue.Create(value))
      let tmpRes = tmpCollection.Find(tmpQuery)
      // For each child, generate all its sub-children recursively
      // and return all such elements as part of this sequence using 'yield!'
      for child in tmpRes do
        yield! children child.Value }

// Using 'List.ofSeq' to fully evaluate the lazy sequence
let resultList = List.ofSeq (children "aParentStartValue")
let rec children(值:string)=seq{
//生成当前值作为序列的下一个元素
收益率
如果有孩子,那么
//如果它有孩子,那么就把所有的孩子都找来
让tmpCollection=database.GetCollection(“Collection”)
让tmpQuery=Query.EQ(“字段”,BsonValue.Create(value))
让tmpRes=tmpCollection.Find(tmpQuery)
//对于每个子级,递归生成其所有子级
//并使用“yield!”将所有这些元素作为该序列的一部分返回
适用于tmpRes do中的儿童
yield!children.Value}
//使用'List.ofSeq'完全评估延迟序列
让resultList=List.ofSeq(子项“aParentStartValue”)

如果树嵌套得更深,那么情况就更困难了。迭代所有子级时,需要将迄今收集的列表传递给第一个子级,获得结果,然后将结果列表传递给下一个子级(使用类似于
list.fold的方法)。但是上面的代码是干净的,在大多数情况下都可以使用。

除非树嵌套得很深(在这种情况下,这将是低效的),否则您可以将代码编写为递归F#序列表达式,使用
yield
yield>生成元素

let rec children (value:string) = seq {
    // Produce the current value as the next element of the sequence
    yield value
    if hasChildren value then 
      // If it has children, then get all the children
      let tmpCollection = database.GetCollection<Collection>("Collection")
      let tmpQuery = Query.EQ("Field",BsonValue.Create(value))
      let tmpRes = tmpCollection.Find(tmpQuery)
      // For each child, generate all its sub-children recursively
      // and return all such elements as part of this sequence using 'yield!'
      for child in tmpRes do
        yield! children child.Value }

// Using 'List.ofSeq' to fully evaluate the lazy sequence
let resultList = List.ofSeq (children "aParentStartValue")
let rec children(值:string)=seq{
//生成当前值作为序列的下一个元素
收益率
如果有孩子,那么
//如果它有孩子,那么就把所有的孩子都找来
让tmpCollection=database.GetCollection(“Collection”)
让tmpQuery=Query.EQ(“字段”,BsonValue.Create(value))
让tmpRes=tmpCollection.Find(tmpQuery)
//对于每个子级,生成其所有子级re