Created
December 30, 2024 16:57
-
-
Save lamg/518408372f3bf829714c29eada6ce77b to your computer and use it in GitHub Desktop.
decent_bfs.fsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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