在下面的代码中,我必须在每次迭代中重复使用 Active Pattern 结果三次。IE
match tree.Parent, postion with
我发现我可以保存活动模式结果。IE
let pos = ((|Root|Nil|Single|First|Inner|Last|Unknown|) (tree.Parent, position))
我不知道是否可以在 match 语句中使用 Active Pattern 结果。IE
match pos with
| ??? -> printf "("
问题是可以在匹配语句中使用保存的活动模式结果吗?
如果是这样,怎么做?如果不是,则需要对其进行解释,以使其在逻辑上有意义。
可能为什么不的例子。即语言规范,句法糖,不应该允许绑定活动模式结果,ExprItems vs. PatItems
我查看了 F# 2.0 语言规范(2010 年 4 月) http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/spec.html#_Toc270597500
但我没有认出任何可以确认答案的东西。
编辑
如果我将代码更改为
let pos = (|Root|Nil|Single|First|Inner|Last|Unknown|) (tree.Parent, position)
match pos with
| Choice1Of7 (tree.Parent, position) -> printf "("
| _ -> ()
在 Choice1Of7 之后,我收到以下错误(tree.Parent,position):
这个表达式应该有单位类型,但这里有类型 'a * 'b
正如布赖恩建议的那样
match pos with
| Choice1Of7 () -> printf "("
| _ -> ()
结束编辑
注意:我在下面尝试了这个代码,但我确实找到了一个更好的算法来解决它。
// An F# function to print a CommonTree as an s-expression
//
// Note: Because the CommonTree data structure was created allowing null values,
// the null value has to be handled.
//
let commonTreeToSExpression100 (tree : BaseTree) =
// Define Active Pattern to create new view of node, i.e. BaseTree
// for position in list instead of content.
// Note: The name of the active pattern function is "|Root|Nil|Single|First|Inner|Last|Unknown|"
let (|Root|Nil|Single|First|Inner|Last|Unknown|) (tree : ITree , position) =
let parent = tree :?> BaseTree
match parent with
| null -> Root
| _ ->
let list = parent.Children
match obj.ReferenceEquals(list,null) with
| true -> Nil // This should never happen.
| false ->
let count = list.Count
// TODO: Handle count = 0
if (count = 1) then Single
elif (count > 1) && (position = 0) then First
elif (count > 1) && (position = count - 1) then Last
elif (count > 1) && (0 < position) && (position < count - 1) then Inner
else Unknown // This should never happen.
// Define walk/print function
let rec printTree (tree : BaseTree) (position) =
// Start an s-expression
match tree.Parent, position with
| Root | Single | First -> printf "("
| _ -> ()
// Note: Can save the result of an active pattern which is type Choice<uint,uint,uint,uint,uint,uint,uint>.
// The question is can the saved active pattern result be used in a match statement?
// If so, how? If not, need to explain it so that it logicaly makes sense.
// Give examples of possibly why not. i.e. Language Specification, syntactic sugar,
// should not have allowed result to be bound, not in symbol table but other table.
//let pos = ((|Root|Nil|Single|First|Inner|Last|Unknown|) (tree.Parent, position)) // This works / is allowed
// match pos with
// | ??? -> printf "(" // Can this work? What should ??? be?
// | _ -> ()
// Visit the node
printf "%s" tree.Text
// Space out the values
match tree.Parent, position with
| Root | First | Inner -> printf " "
| _ -> ()
// Process the children
// Note: BaseTree holds the ChildIndex, if this property was not available
// then the use of Seq.iter whould have to be changed for a mechanism that
// would allow the calculation of the child index as the list is processed.
match tree.Children with
| null -> ()
| _ ->
tree.Children |> Seq.iter (fun x -> printTree (x :?> BaseTree) x.ChildIndex)
printf " "
// End an s-expression
match tree.Parent, position with
| Root | Single | Last -> printf ")"
| _ -> ()
// Call the walk/print function
printTree tree 0
// Since s-experssions print as single line,
// print a newline so that what is printed after this
// is not on the same line as this s-expression.
printfn ""