Friends, the following code runs fine when using Seq.unfold. However, List.unfold or Array.unfold (as shown below) causes the program to never terminate. I'm mostly just curious as to why that is. However, I am biased in general towards only using Arrays. Can anyone explain what is the reason for this behavior and if possible how to work within the confines of Arrays for a problem with this general structure.
open MathNet.Numerics.LinearAlgebra
open MathNet.Numerics.Distributions
let randn() = Normal.Sample(0., 1.)
let N = 100
let y = DenseVector.init N (fun _ -> 10. + sqrt(1.) * randn())
let SIM =
Array.unfold (fun (c1_, c2_) ->
let D = 1./(1. / 100. + float(N) / c2_)
let c1 = D *(0. / 100. + y.Sum() / c2_) + sqrt(D) * randn()
let a1 = (3. + float(N) / 2.)
let a2 = (0.5 + ((y-c1).PointwisePower(2.)).Sum() / 2.)
let c2 = InverseGamma.Sample(a1, a2)
Some((c1_, c2_), (c1, c2))
) (0., 1.)
|> Array.take (100000)
let result = SIM |> Array.map (fun (i, j) -> i)