2 people like it.

Rabbits and Recurrence Relations

One solution to Rosalind rabbits problem.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
type population = { Adults:int; Newborn:int }
let rabbits n k =
    let rec evalPopulation pop gen =
        match pop with
        | _ when gen = n -> pop
        | { Adults=ad; Newborn=nb } -> evalPopulation { Adults=ad+nb; Newborn=ad*k } (gen+1)
    evalPopulation { Adults=0; Newborn=1} 1

rabbits 5 3
|> printfn "%A"
population.Adults: int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
population.Newborn: int
val rabbits : n:int -> k:int -> population

Full name: Script.rabbits
val n : int
val k : int
val evalPopulation : (population -> int -> population)
val pop : population
val gen : int
val ad : int
val nb : int
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Raw view New version

More information

Link:http://fssnip.net/lQ
Posted:3 years ago
Author:Michel Caradec
Tags: rosalind , recursion