I have a function getFullFitness
let getFullFitness population =
ResizeArray(population |> Seq.map snd) |> Seq.sum
and I pattern match in the function realTick
let realTick population =
match(getFullfitness population) with
|(50) -> population
| _ -> childGeneration population
Question is on the line |(50) -> population. Since getFullFitness returns an integer sum, how do i match values between 0 and 50 in realTick?
One way is to use a guard -
|t when t < 50 -> ...
In F#, a recommended way to pattern match on ranges is to use active patterns. If we carefully manipulate pattern names, it will look quite close to what we want:
let (|``R0..50``|_|) i =
if i >= 0 && i <= 50 then Some() else None
let realTick population =
match(getFullfitness population) with
| ``R0..50`` -> population
| _ -> childGeneration population
Pattern matching on ranges is supported in OCaml, but it's unlikely to be added to F#. See the related User Voice request at http://fslang.uservoice.com/forums/245727-f-language/suggestions/6027309-allow-pattern-matching-on-ranges.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With