F#:不理解匹配 .. 与 [英] F#: Not understanding match .. with

查看:23
本文介绍了F#:不理解匹配 .. 与的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在搞 F# 和 Fable,并试图测试我的理解.为此,我尝试创建一个函数来计算给定一定迭代次数的 e.我想到的是

I'm messing around with F# and Fable, and trying to test my understanding. To do so, I tried creating a function to calculate e given a certain number of iterations. What I've come up with is

let eCalc n =
      let rec internalECalc ifact sum count =
          match count = n with
          | true -> sum
          | _ -> internalECalc (ifact / (float count)) (sum + ifact) (count+1)

      internalECalc 1.0 0.0 1

哪个工作正常,调用时返回 2.7182818284590455

Which works fine, returning 2.7182818284590455 when called with

eCalc 20

但是,如果我尝试使用,我认为是更正确的形式

However, if I try using, what I think is, the more correct form

let eCalc n =
      let rec internalECalc ifact sum count =
          match count with
          | n -> sum
          | _ -> internalECalc (ifact / (float count)) (sum + ifact) (count+1)

      internalECalc 1.0 0.0 1

我收到警告[警告] 此规则永远不会匹配 (L5,10-L5,11)",并返回值 0.(如果我交换 'n' 和 'count',也会发生同样的事情在匹配语句中).有什么原因我不能在 match 语句中使用 'n' 吗?有没有办法解决这个问题,以便我可以使用n"?

I get a warning "[WARNING] This rule will never be matched (L5,10-L5,11)", and returned value of 0. (and the same thing happens if I swap 'n' and 'count' in the match statement). Is there a reason I can't use 'n' in the match statement? Is there a way around this so I can use 'n'?

谢谢

推荐答案

当您在 match 语句中使用名称时,您没有根据值检查它按照您认为的方式分配给该变量.您正在指定该名称.即,

When you use a name in a match statement, you're not checking it against the value assigned to that variable the way you think you are. You are instead assigning that name. I.e.,

match someInt with
| n -> printfn "%d" n

将打印 someInt 的值.它相当于 let n = someInt;printfn "%d" n.

will print the value of someInt. It's the equivalent of let n = someInt; printfn "%d" n.

您想要做的是使用 when 子句;在 when 子句中,您不是模式匹配,而是执行标准" if 检查.所以你想要的是:

What you wanted to do was use a when clause; inside a when clause, you're not pattern-matching, but doing a "standard" if check. So what you wanted was:

let eCalc n =
      let rec internalECalc ifact sum count =
          match count with
          | cnt when cnt = n -> sum
          | _ -> internalECalc (ifact / (float count)) (sum + ifact) (count+1)

      internalECalc 1.0 0.0 1

这是否有意义,或者您需要我详细说明吗?

Does that make sense, or do you need me to go into more detail?

附言在这种情况下,您的匹配函数看起来像x when(涉及 x 的布尔条件)-> case 1 | _ -> case 2",使用简单的 if 表达:

P.S. In a case like this one where your match function looks like "x when (boolean condition involving x) -> case 1 | _ -> case 2", it's quite a bit more readable to use a simple if expression:

let eCalc n =
      let rec internalECalc ifact sum count =
          if count = n then
              sum
          else
              internalECalc (ifact / (float count)) (sum + ifact) (count+1)

      internalECalc 1.0 0.0 1

这篇关于F#:不理解匹配 .. 与的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆