I'm not sure how to remove the cycles from a mutable list of type:
type 'a m_list = Nil | Cons of 'a * (('a m_list) ref)
E.g. if I had a list 3,2,2,1,2,1,2,1,..... I would want to get a 3,2,2,1.
What I can't figure out is the location of the initial cycling--I have a recursion that looks like this but I can't figure out how to wrap this into a recursive function; obviously here it would just checks the first few terms.
let remove list : unit =
if is_cyclic list then match list with
|Nil->()
|Cons(_,v)-> match (!v) with
|Nil->()
|Cons(_,x)->match (!x) with
|Nil->()
|Cons(_,y)->match (!y) with
|Nil->()
|Cons(_,p) -> if is_cyclic (!p) then p:=Nil else ()
I have an is_cyclic function that tells me whether an m_list has a cycle or not. I'd like to do this either destructively (updating the reference) or indestructively (creating a new list).
Thanks!