2011-09-04 79 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”子句相似吗?