Is this idiomatic F# for a fairly quick infinite recursive sequence?

Viewed 343

Whilst solving the 12th project euler problem I set about making an infinite sequence to get successive triangular numbers. My first attempt was:

let slowTriangularNumbers =

  let rec triangularNumbersFrom n = 
    seq { 
      yield seq { 0 .. n } |> Seq.sum
      yield! triangularNumbersFrom (n + 1) 
    }

   triangularNumbersFrom 1

This turned out to be very slow - each time getting the next item it had to compute all of the additions leading up to n.

My next attempt was:

let fasterTriangularNumbers =

  let cache = System.Collections.Generic.Dictionary<int, int>()
  cache.[0] <- 0         

  let rec triangularNumbersFrom n = 
  seq { 
    cache.[n] <- cache.[n - 1] + n                   
    yield cache.[n]
    yield! triangularNumbersFrom (n + 1) 
  }

  triangularNumbersFrom 1

Introducing the mutable dictionary has speeded it up considerably, but is this normal to have a mutable collection, or is there another way I could have represented state?

1 Answers
Related