Tree Representation in F#

Viewed 6768

I'm trying to implement a tree in F# using a list of tuples.
[a] where a = (string, [a])
Each node has a list of their children and leaf nodes would be (name, [])

I want to be able to recursively iterate through each level of the list like this.

    a
 b     e
c d   f g

They wont always be binary trees however.

let t2 = [("a", [("b", [("c", []), ("d", [])]), ("e", [("f", []), ("g", [])])])]

let rec checkstuff tple =
    match tple with
    | (_, []) -> true
    | (node, children) ->
        List.fold ( || ) false (List.map checkstuff children)

I get:

Type mismatch. Expecting a
    ('a * 'b list) list
but given a
    'b list
The resulting type would be infinite when unifying ''a' and ''b * 'a list'

Is there a way I can do something like this or is there not support for a recursive list of tuples like this?

2 Answers
Related