繁体   English   中英

F#有什么像Haskell的where子句吗?

[英]Has F# anything like Haskell's where clause?

我想知道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#的关键词引用似乎没有像我正在寻找的那样展示。 如果它不存在,有没有其他方法可以更好地分解显示的代码? 我会说它没关系,但你永远都不会知道......

可悲的是没有 - 更令人遗憾的是,F#的订单很重要。 你可以使用相互递归let rec ... and ...但它与where在哪里不一样。

F#中没有任何这样的关键字。 两个代码的区别在于,在一个定义中首先使用然后使用,在第二个定义中反之亦然。

最近版本的F#中的“in”关键字不是类似于Haskell的“where”子句吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM