F# 如何将复杂表达式传递给参数化的活动模式?

F# 如何将复杂表达式传递给参数化的活动模式?,f#,active-pattern,F#,Active Pattern,我将活动模式“表达式”定义如下: let (|Expression|_|) expression _ = Some(expression) 现在我试着这样使用它: match () with | Expression((totalWidth - wLeft - wRight) / (float model.Columns.Count - 0.5)) cw when cw <= wLeft * 4. && cw <= wRight * 4. ->

我将活动模式“表达式”定义如下:

let (|Expression|_|) expression _ = Some(expression)
现在我试着这样使用它:

match () with
| Expression((totalWidth - wLeft - wRight) / (float model.Columns.Count - 0.5)) cw
    when cw <= wLeft * 4. && cw <= wRight * 4. ->
        cw
| Expression((totalWidth - wLeft) / (float model.Columns.Count - .25)) cw
    when cw <= wLeft * 4. && cw > wRight * 4. ->
        cw
| Expression((totalWidth - wRight) / (float model.Columns.Count - .25)) cw
    when cw > wLeft * 4. && cw <= wRight * 4. ->
        cw
| Expression(totalWidth / float model.Columns.Count) cw
    when cw > wLeft * 4. && cw > wRight * 4. ->
        cw
| _ -> System.InvalidProgramException() |> raise
match()与
|表达式((totalWidth-wLeft-wRight)/(float model.Columns.Count-0.5))cw
当cw
连续波
|表达式((totalWidth-wRight)/(float model.Columns.Count-.25))cw
当cw>wLeft*4.&连续波
连续波
|表达式(totalWidth/float model.Columns.Count)cw
当cw>wLeft*4.&cw>wRight*4.->
连续波
|->System.invalidProgrameException()|>raise
但这会导致“错误FS0010:模式中的意外符号“-”。这可以解决吗

我想做的是清楚地写出以下等式的解:

最大值(wl-cw*.25,0)+最大值(wr-cw*.25)+cw*列数=实际宽度

其中cw是唯一的变量


你能提出更好的方法吗?

可以用作参数化活动模式参数的表达式的语言在某些方面是有限的。据我所知,没有明确地说,但语法表明必须能够将参数表达式解析为
pat param
(第90页):

pat参数:=
|常数
|长标识符
|[pat param;…;pat param]
|(帕特参数,…,帕特参数)
|长标识pat参数
|pat参数:类型
|
|
|空的

因此,我认为您需要以不同的方式编写模式匹配。您可以将表达式转换为
match
构造的普通参数,并编写如下内容:

match 
  (totalWidth - wLeft - wRight) / (float model.Columns.Count - 0.5),
  (totalWidth - wLeft) / (float model.Columns.Count - .25),
  (totalWidth - wRight) / (float model.Columns.Count - .25)
with
| cw1, _, _ when cw1 <= wLeft * 4. && cw1 <= wRight * 4. -> cw1
| _, cw2, _ when cw2 <= wLeft * 4. && cw2 > wRight * 4. -> cw2
| _, _, cw3 when cw3 > wLeft * 4. && cw3 <= wRight * 4. -> cw3
| _ -> totalWidth / float model.Columns.Count
。。。然后写下如下内容:

let wDif = wLeft - wRight
match () with
| Calculate wDif 0.5 cw -> cw
| Calculate wLeft 0.25 cw -> cw
// .. etc.

那么,是否不可能使用复合函数作为活动模式的参数?e、 g.我们不能做
|MyActive(myfuna>>myfunb)x->。
我们只能让
myfun=myfuna>>myfunb…|MyActive myfun x->…
是的,我认为这是正确的-您不能直接使用任何复杂表达式。
let wDif = wLeft - wRight
match () with
| Calculate wDif 0.5 cw -> cw
| Calculate wLeft 0.25 cw -> cw
// .. etc.