F#: Not understanding match .. with

我只是一个虾纸丫 提交于 2019-12-17 21:32:13

问题


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

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

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'?

Thanks


回答1:


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

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

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?

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


来源:https://stackoverflow.com/questions/38711655/f-not-understanding-match-with

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!