重用F#活动模式结果

时间:2012-03-12 23:57:27

标签: f# active-pattern

在下面的代码中,我必须为每次迭代重复使用Active Pattern结果三次。即

match tree.Parent, postion with

我发现我可以保存活动模式结果。即

let pos = ((|Root|Nil|Single|First|Inner|Last|Unknown|) (tree.Parent, position)) 

我无法弄清楚的是,是否可以在匹配语句中使用活动模式结果。即

match pos with
| ??? -> printf "("

问题是可以在匹配语句中使用保存的活动模式结果吗?

如果是这样,怎么样?如果没有,需要解释它,以便逻辑上有意义。

可能为什么不这样做的例子。即语言规范,语法糖, 不应该允许绑定活动模式结果,ExprItems与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

正如Brian建议的那样

    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 ""

1 个答案:

答案 0 :(得分:5)

它不是特别优雅,但您可以使用用于表示活动模式的基础区分联合。使用N个选项的活动模式的结果使用类型Choice<'T1, .., 'Tn>表示,其中包含Choice1OfN .. ChoiceNOfN成员。

这是一个简单的例子,只有三种情况:

let (|Pos|Neg|Zero|) n = 
  if n < 0 then Neg (-n)
  elif n = 0 then Zero
  else Pos n

let res = (|Pos|Neg|Zero|) 10

match res with
| Choice1Of3 n -> sprintf "pos %d" n
| Choice2Of3 n -> sprintf "neg %d" n
| Choice3Of3 () -> "zero"

在实践中,我可能不会使用这种方法,但我会定义一个自定义区分联盟:

type Number = Pos of int | Neg of int | Zero

let convertNumber n = 
  if n < 0 then Neg (-n)
  elif n = 0 then Zero
  else Pos n

let res = convertNumber 10

match res with
| Pos n -> sprintf "pos %d" n
| Neg n -> sprintf "neg %d" n
| Zero -> "zero"

这需要明确定义一个有区别的联合,但它使代码更具可读性。