如何将复杂表达式传递给参数化活动模式?
我将活动模式“表达式”定义如下:
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 是唯一的变量。
你能建议更好的方法吗?
I defined the active pattern "Expression" as follows:
let (|Expression|_|) expression _ = Some(expression)
Now I'm trying to use it in this way:
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
But this results in "error FS0010: Unexpected symbol '-' in pattern". Is that fixable?
What am I trying to do is to write clearly a solution to the following equation:
max(wl - cw * .25, 0) + max(wr - cw * .25) + cw * columnCount = ActualWidth
where cw is the only variable.
Can you suggest any better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可用作参数化活动模式参数的表达式语言在某些方面受到限制。据我所知,F# 规范< /a> 没有明确说明这一点,但语法表明必须可以将参数表达式解析为
pat-param
(第90页):所以,我认为您需要以不同的方式编写模式匹配。您可以将表达式转换为
match
构造的普通参数,并编写如下内容:如果表达式中使用的模式始终相同,您还可以使用活动模式,例如:
... 然后写一些像:
The langauge of expressions that can be used as arguments for parameterized active patterns is limited in some ways. As far as I can tell, the F# specification doesn't say that explicitly, but the grammar suggests that it must be possible to parse the argument expression as
pat-param
(page 90):So, I think you'll need to write your pattern matching differently. You could turn the expressions into ordinary arguments of the
match
construct and write something like this:If the pattern used in the expression is always the same, you could also use active pattern like:
... and then write something like: