I found a great haskell solution ( source ) to generate a Hofstadter sequence :
hofstadter = unfoldr (\(r:s:ss) -> Just (r, r+s:delete (r+s) ss)) [1..]
Now I am also trying to write such a solution in F #. Unfortunately (I'm not very happy with F #), I have not been successful so far.
My problem is that when I use sequence in F #, it seems that I will not be able to delete the element (as is done in the haskell solution).
Other data structures, such as arrays , list or set , which allow you to delete items, do not generate an infinite sequence, but only work with specific items.
So my question is: Is it possible in F # to generate an infinite sequence where the elements are deleted?
Some things I've tried so far:
Infinite sequence of numbers:
let infinite = Seq.unfold( fun state -> Some( state, state + 1) ) 1
Hofstadter sequence - does not work because there is no del keyword and there are more syntax errors
let hofstadter = Seq.unfold( fun (r :: s :: ss) -> Some( r, r+s, del (r+s) ss)) infinite
I was thinking about using Seq.filter but could not find a solution.
source share