Code Snippet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| #load @"Utils/ListLazy.fs"
open Stermon.Research.Utils
let corruptStartPoint l =
let rec loopStart = function
| Cons(h1,t1),Cons(h2,t2) -> (h1 = h2) |> function
| true -> Some h1
| false ->
loopStart (t1.Force(),t2.Force())
| _,_ -> None
let rec meetPoint = function
| Cons(h1,t1),Cons(h2,t2) -> (h1 = h2) |> function
| true -> loopStart (l,t2.Force())
| false ->
meetPoint (t1.Force(),(t2.Force() |> List.Lazy.skip 1I))
| _,_ -> None
meetPoint (l,(l |> List.Lazy.skip 1I))
|
Code output:
Non corrupt linked list
1
2
3
4
5
6
| let example1 = List.Lazy.unfold(fun s -> Some(s,s+1)) 0 |> List.Lazy.take 26I
example1
|> List.Lazy.iter(printf "%i ")
corruptStartPoint example1
|
Corrupt linked list
1
2
3
4
5
6
7
8
9
10
11
12
13
| let corruptGenerator n =
List.Lazy.unfold(
fun s -> (s < n) |> function
| false -> Some((s % n) + n,s+1)
| true -> Some(s,s+1)) 0
let example2 = corruptGenerator 7
example2
|> List.Lazy.take 28I
|> List.Lazy.iter(printf "%i ")
corruptStartPoint example2
|
References: