2008-11-18 72 views
0

我遇到語法錯誤。 我想發表一個返回浮點數的函數。F#語法錯誤

以爲這會給我正確的答案

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

但事實並非如此。我嘗試了所有我能想到的事情,但這只是爲了我。我知道這是愚蠢的,但我想不起來。

作爲參考,fudge需要一個float和一個整數,cyclesPerWord需要2個整數,而wordsPerSec需要2個整數。 Floor採用泛型並返回一個浮點數。

回答

3

還要注意的是,您可以使用parens以您最初嘗試的方式嵌套函數調用,例如,

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

(無內設置上面括號的,它有點像你試圖傳遞4個參數來cyclesPerWord,這是不是你想要的。)

3

另外,爲了避免讓失明和括號麻痹,使用一些流水線|>:

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 
0

你的函數定義來看,好像你使用的是C#的語法調用你的函數,函數名存在權前()和相關參數該函數在()內。一個例子是FunctionName(Parameter1 Parameter2)。 F#不使用該風格。相反,它使用()中存在函數名稱和相關參數的樣式。一個例子是(FunctionName Parameter1 Parameter2)。

來表達你的代碼正確的方法是

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

雖然最外面的()是不是真的有必要。

+0

Aaah。我一直在試圖弄清楚。 – 2008-12-12 23:33:44