2011-09-04 32 views
7

我想知道F#中是否有像Haskell的where子句中的任何內容。這將允許以變換以下代碼F#與Haskell的where子句一樣嗎?

let roulleteWheel numberGenerator (scoredPopulation:ScoredPopulation) = 
    let targetScore = 
    let totalScore = totalPopulationFitness scoredPopulation 
    Seq.head (numberGenerator 0.0 totalScore) 

    let isMatch (score, accumulatedScore) = 
    if (accumulatedScore >= targetScore) then 
     Some(score) 
    else 
     None 

    let accumulatedScores = 
    let scores = Seq.map (fun (_, score) -> score) scoredPopulation 
    Seq.skip 1 (Seq.scan (+) 0.0 scores) 

    Seq.pick isMatch (Seq.zip scoredPopulation accumulatedScores) 

入(IMO)稍微更可讀的版本

let roulleteWheel numberGenerator (scoredPopulation:ScoredPopulation) = 
    Seq.pick isMatch (Seq.zip scoredPopulation accumulatedScores) 
    where 
    let targetScore = 
     let totalScore = totalPopulationFitness scoredPopulation 
     Seq.head (numberGenerator 0.0 totalScore) 

    let isMatch (score, accumulatedScore) = 
     if (accumulatedScore >= targetScore) then 
     Some(score) 
     else 
     None 

    let accumulatedScores = 
     let scores = Seq.map (fun (_, score) -> score) scoredPopulation 
     Seq.skip 1 (Seq.scan (+) 0.0 scores) 

,因爲它顯示了功能的核心部分的第一和實現細節以後(挑剔,我認爲!)。

我的猜測是F#解析代碼文件的方式是不可能的。我對嗎?看看F#的關鍵字引用似乎沒有炫耀我想要的任何東西。如果它不存在,有沒有其他方法可以更好地分解出所顯示的代碼?我會說這是好的,但你永遠不會知道..

回答

6

可悲的是沒有 - 更令人難過的訂單在F#事項很多。 你可以使用相互遞歸的let rec ... and ...,但它與where不一樣。

1

F#中沒有任何這樣的關鍵字。這兩個代碼的區別在於,其中一個定義首先使用,然後在第二個使用中,反之亦然。

0

不是最近版本的F#中的「in」關鍵字與Haskell的「where」子句相似嗎?