F# 语法错误

发布于 2024-07-08 17:02:46 字数 368 浏览 8 评论 0原文

我遇到语法错误。 我想获取返回浮点数的函数的下限。

认为这会给我正确的答案

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

,但事实并非如此。 我已经尝试了我能想到的一切,但对我来说还是不行。 我知道这是愚蠢的事情,但我想不出来。

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

I'm having a syntax error.
I want to take the floor of a function that returns a floating point number.

I thought this would give me the right answer

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

But it doesn't. I've tried everything I can think of, and it's just not coming together for me. I know it's something stupid, but I can't think of it.

For reference, fudge takes a float and an integer, cyclesPerWord takes 2 integers and wordsPerSec takes 2 integers. Floor takes a generic and returns a float.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烛影斜 2024-07-15 17:02:46

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

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

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

Note also that you can use parens to nest the function calls the way you were originally trying to, e.g.

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

(Without the inner set of parens above, it's kinda like you're trying to pass 4 arguments to cyclesPerWord, which is not what you want.)

不语却知心 2024-07-15 17:02:46

或者,为了避免盲目性和括号瘫痪,请使用一些管道 |> :

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

Alternatively, to avoid let blindness and parenthesis paralysis, use some pipelining |> :

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
往事随风而去 2024-07-15 17:02:46

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

表达代码的正确方法是,

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

尽管最外面的 ( ) 并不是真正必要的。

Looking at your function definition, it seems like you are using a C# like syntax for calling your functions, the function name exists right before the ( ) and the associated parameters for that function are within the ( ). An example would be FunctionName(Parameter1 Parameter2). F# doesn't use that style. Instead it uses a style where the function name and associated parameters exist inside the ( ). An example of this would be (FunctionName Parameter1 Parameter2).

The correct way to express your code would be

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

though the outermost ( ) are not really necessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文