Skip to content

Instantly share code, notes, and snippets.

@lamg
Created December 30, 2024 16:57
Show Gist options
  • Save lamg/518408372f3bf829714c29eada6ce77b to your computer and use it in GitHub Desktop.
Save lamg/518408372f3bf829714c29eada6ce77b to your computer and use it in GitHub Desktop.
decent_bfs.fsx
type Tree<'a> = { value: 'a; children: Tree<'a> list }
let rec levels (ts: Tree<'a> list) =
match ts with
| [] -> []
| _ ->
let xs, chls = ts |> List.map (fun t -> t.value, t.children) |> List.unzip
let subs = chls |> List.concat |> levels
xs :: subs
[ { value = 1
children =
[ { value = 2
children =
[ { value = 5
children = [ { value = 6; children = [] } ] } ] }
{ value = 3
children =
[ { value = 4
children = [ { value = 7; children = [] } ] } ] } ] } ]
|> levels
|> List.iter (fun xs -> printfn $"{xs}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment