【问题标题】:F# syntax errorF# 语法错误
【发布时间】:2008-11-18 02:16:34
【问题描述】:

我遇到语法错误。 我想讨论一个返回浮点数的函数。

认为这会给我正确的答案

let cyclesPerInterrupt bps bpw cpu factor = 
 floor (fudge (float(factor) cyclesPerWord cpu wordsPerSec bps bpw))

但事实并非如此。我已经尝试了所有我能想到的东西,但对我来说还是没用。我知道这很愚蠢,但我想不出来。

作为参考,fudge 采用浮点数和整数,cyclesPerWord 采用 2 个整数,wordsPerSec 采用 2 个整数。 Floor 接受一个泛型并返回一个浮点数。

【问题讨论】:

    标签: f# syntax-error function-calls


    【解决方案1】:

    另请注意,您可以使用括号以您最初尝试的方式嵌套函数调用,例如

    ...(cyclesPerWord cpu (wordsPerSec bps bpw))
    

    (如果没有上面的内部括号集,这有点像您试图将 4 个参数传递给 CyclesPerWord,这不是您想要的。)

    【讨论】:

      【解决方案2】:

      另外,为了避免盲目和括号瘫痪,使用一些流水线 |> :

      let fudge (a : float) (b : int) =
          a
      
      let cyclesPerWord (a : int) (b : int) =
          a
      
      let wordsPerSec (a : int) (b : int) =
          a
      
      let cyclesPerInterrupt bps bpw cpu factor =
          wordsPerSec bps bpw
          |> cyclesPerWord cpu
          |> fudge factor
          |> floor
      

      【讨论】:

        【解决方案3】:

        查看您的函数定义,您似乎正在使用类似 C# 的语法来调用您的函数,函数名称位于 () 之前,并且该函数的相关参数位于 () 内。一个例子是 FunctionName(Parameter1 Parameter2)。 F# 不使用那种风格。相反,它使用函数名称和相关参数存在于 () 中的样式。这方面的一个例子是 (FunctionName Parameter1 Parameter2)。

        表达代码的正确方法是

          let cyclesPerInterrupt bps bpw cpu factor = 
            (floor (fudge (float factor) (cyclesPerWord cpu (wordsPerSec bps bpw) ) ) )
        

        虽然最外面的 ( ) 并不是真正需要的。

        【讨论】:

        • 啊啊。我一直在努力解决这个问题。
        猜你喜欢
        • 2018-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-20
        • 2021-01-19
        • 2021-02-01
        • 2021-09-11
        • 2022-10-25
        相关资源
        最近更新 更多