Last active
August 29, 2015 13:56
-
-
Save Tamschi/8869829 to your computer and use it in GitHub Desktop.
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> (root : 'a, children : 'a tree list) = | |
new (root, [<System.ParamArray>]children) = tree(root, List.ofArray children) | |
member this.Root = root | |
member this.Children = children | |
let rec lines | |
format | |
(t : 'a tree) | |
: string seq | |
= | |
seq { | |
yield sprintf format t.Root | |
if t.Children.Length > 0 then | |
let cLines spacer c = seq { | |
let cLines = Array.ofSeq (lines format c) | |
yield fst spacer + cLines.[0] | |
for cl in Seq.skip 1 cLines -> snd spacer + cl | |
} | |
let cs = Array.ofList t.Children | |
for i = 0 to t.Children.Length - 2 do | |
yield! cLines ("├", "│") t.Children.[i] | |
yield! cLines ("└", " ") t.Children.[t.Children.Length - 1] | |
} | |
let t = | |
tree (1, | |
tree (2, | |
tree 4, | |
tree 6), | |
tree (3, | |
tree 6, | |
tree 9), | |
tree (4, | |
tree 8)) | |
for l in (lines "%A" t) do | |
printfn "%s" l |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment