3

アクティブパターン「式」を次のように定義しました。

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

ただし、これにより「エラーFS0010:パターン内の予期しないシンボル'-'」が発生します。それは修正可能ですか?

私がやろうとしているのは、次の方程式の解を明確に書くことです。

max(wl --cw * .25、0)+ max(wr --cw * .25)+ cw * columnCount = ActualWidth

ここで、cwは唯一の変数です。

もっと良い方法を提案できますか?

4

1 に答える 1

7

パラメータ化されたアクティブパターンの引数として使用できる式の言語は、いくつかの点で制限されています。私の知る限り、F#仕様では明示的には述べられていませんが、文法では、引数式を次のように解析できる必要があることを示していpat-paramます(90ページ)。

pat-param:=
    | const
    | ロングアイデンティティ
    | [ pat-param ; ...; pat-param ]
    | (pat-param、...、pat-param
    | long-ident pat-param
    | pat-paramタイプ
    | <@ expr @>
    | <@@ expr @@>
    | ヌル

したがって、パターンマッチングを別の方法で記述する必要があると思います。式を構成の通常の引数に変換して、次の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 (|Calculate|) w p _ =
  (totalWidth - w) / (float model.Columns.Count - p)

...そして次のように書きます:

let wDif = wLeft - wRight
match () with
| Calculate wDif 0.5 cw -> cw
| Calculate wLeft 0.25 cw -> cw
// .. etc.
于 2011-05-29T17:05:38.150 に答える