简体   繁体   English

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

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

I was wondering if there is anything in F# like Haskell's where clause. 我想知道F#中是否有任何东西像Haskell的where子句。 It would permit to transform the following code 它将允许转换以下代码

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)

into the (imo) slightly more readable version 进入(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)

as it shows up the core part of the function first and the implementation details later (nitpicking, I reckon!). 因为它首先显示了函数的核心部分,后来又显示了实现细节(我猜错了!)。

My guess is that it wouldn't be possible by the way F# parses the code files. 我的猜测是,F#解析代码文件的方式是不可能的。 Am I right? 我对吗? Looking at F#'s keywords reference doesn't seem to show off anything like I'm looking for. 看看F#的关键词引用似乎没有像我正在寻找的那样展示。 If it doesn't exist, is there any other way to better factor out the code shown? 如果它不存在,有没有其他方法可以更好地分解显示的代码? I'd say it is ok as it is, but you never know.. 我会说它没关系,但你永远都不会知道......

Sadly no - even more sadly order in F# matters a lot. 可悲的是没有 - 更令人遗憾的是,F#的订单很重要。 You can use mutual recursive let rec ... and ... but it's just not the same as where. 你可以使用相互递归let rec ... and ...但它与where在哪里不一样。

There isn't any such keyword in F#. F#中没有任何这样的关键字。 The difference in both the code is that in one the definition comes first and then usage and in the 2nd one it is vice-versa. 两个代码的区别在于,在一个定义中首先使用然后使用,在第二个定义中反之亦然。

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

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

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